locked
Template = word doc...How can i select the template name from a combobox and display the contents of the word doc in a richtextbox? RRS feed

  • Question

  • I know this one is going to be hard...but i have faith in our MSDN!!! so here is the idea...i have a form that is triggered by a button click.  this form is a basic email form that i have created...it has different textboxes.. textbox1= sent to... textbox2= CC combobox1 = a list of pre-made "template names"

    textbox1 and textbox2 are auto populated...so then the user clicks on a template from the combobox(which i would like linked to a doc somewhere) ...and it populates a richtextbox at the bottom of the form..

    1...How do i link a name...to a word document?(or .txt if i have to "downgrade")

    2.  How after selecting the name, populate he richtextbox with the word docs contents?

    (would it be easier to add a datatable?) (and just string the name to the corrisponding table?) either or i still dont even know how to do that lol

    HELP!!!!

     


    Loving life since 1981 Preston Lambeth
    Tuesday, April 19, 2011 12:55 AM

Answers

  • What i did was create an access database....then added 3 columns...and the last one is called body..and its fomated to memo style...

    so when you select the value out of the combobox..it populates the richtextbox.....ill show you... I thank you for your response

     public email()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          OutLook.Application mailApp = new OutLook.Application();
          OutLook.NameSpace myNam = mailApp.GetNamespace("MAPI");
    
          myNam.Logon(null, null, true, true);
    
    
          OutLook.MAPIFolder ofold = myNam.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderSentMail);
          OutLook._MailItem mi = (OutLook._MailItem)mailApp.CreateItem(OutLook.OlItemType.olMailItem);
          mi.From = "servicedesk.canla@honeywell.com";
          mi.To = mailto.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.Subject = subject.Text;
          mi.HTMLBody = body.Text;// i'm building the body in a sepparate call. It can be empty as well
          mi.SaveSentMessageFolder = ofold;
          mi.Send();
    
    
        }
    
        private void template_SelectedIndexChanged(object sender, EventArgs e)
        {
          //This is the SQL query
          //We check the table Landesk 
          //Note: I had problems with the tables whose names had spaces so please make them one long word
          string aux = template.Text;
          string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
                      Data Source= " + Application.StartupPath + @"\templatetest.accdb;Persist Security Info=False";
          string queryString = @"SELECT * FROM templates WHERE template = " + "'" + aux + "'";
          //string queryString = @"SELECT * FROM people WHERE eid = 'E522252'";
    
          using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))
          {
            System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(queryString, connection);
            //we open the database
            connection.Open();
            //we execute the previous query
            System.Data.OleDb.OleDbDataReader reader = command.ExecuteReader();
    
            //this loop will stop until all the rows found have been read
            while (reader.Read())
            {
              //Dont forget to update this section because of the table changes made!!
    
              if (!(reader.IsDBNull(2)))
                this.subject.Text = reader.GetString(2);
              if (!(reader.IsDBNull(3)))
                this.body.Text = reader.GetString(3);//this is where the access db came in really handy!! 
            }
            // always call Close when done reading.
            reader.Close();
          }
        }
      }
    }
    //so everyone this is one way to do what i wanted to do....thanks for the help!

    Loving life since 1981 Preston Lambeth
    • Marked as answer by Ptiwstp1 Tuesday, April 19, 2011 9:25 PM
    Tuesday, April 19, 2011 9:23 PM

All replies

  • Hi,

    first of all I would not recommend to use a word document. Word documents are not an common format used by emails. (It might be ok to send word documents out but even that is a practice where you should think twice! Maybe you know the receiver and you know that they are working with word but it can easily happen that your word document contains much more than you want to send.)

    If you want to send the content of a word document: Print it to a pdf first and send the pdf.

    But that is not your core requirement in your question.

    I would recommend to use either text files or HTML files. (But at the moment I do not have a nice control ready for html editing, so get it working with text files first).

    When you populate the listbox, you can simply get a list of all files in a directory and just take the names of all text files and add them to your listbox. (e.g. Directory.GetFiles(string, string) method, ListBox.Items.Add() method for Windows Forms, ...)

    The ListBox has a Click event. You can use this click event to do take teh name of the selected item, open that file and then read all content. The read Content is simply stored in the TextBox.Text property. (So the File.ReadAllText method could be used)

    Was that what you was looking for? Where exactly do you have a problem? Maybe you can give more details so we can help directly at the problems and explain missing things to you.

    With kind regards,

    Konrad

    Tuesday, April 19, 2011 10:57 AM
  • What i did was create an access database....then added 3 columns...and the last one is called body..and its fomated to memo style...

    so when you select the value out of the combobox..it populates the richtextbox.....ill show you... I thank you for your response

     public email()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          OutLook.Application mailApp = new OutLook.Application();
          OutLook.NameSpace myNam = mailApp.GetNamespace("MAPI");
    
          myNam.Logon(null, null, true, true);
    
    
          OutLook.MAPIFolder ofold = myNam.GetDefaultFolder(OutLook.OlDefaultFolders.olFolderSentMail);
          OutLook._MailItem mi = (OutLook._MailItem)mailApp.CreateItem(OutLook.OlItemType.olMailItem);
          mi.From = "servicedesk.canla@honeywell.com";
          mi.To = mailto.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.CC = emailcc.Text;
          mi.Subject = subject.Text;
          mi.HTMLBody = body.Text;// i'm building the body in a sepparate call. It can be empty as well
          mi.SaveSentMessageFolder = ofold;
          mi.Send();
    
    
        }
    
        private void template_SelectedIndexChanged(object sender, EventArgs e)
        {
          //This is the SQL query
          //We check the table Landesk 
          //Note: I had problems with the tables whose names had spaces so please make them one long word
          string aux = template.Text;
          string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;
                      Data Source= " + Application.StartupPath + @"\templatetest.accdb;Persist Security Info=False";
          string queryString = @"SELECT * FROM templates WHERE template = " + "'" + aux + "'";
          //string queryString = @"SELECT * FROM people WHERE eid = 'E522252'";
    
          using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))
          {
            System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand(queryString, connection);
            //we open the database
            connection.Open();
            //we execute the previous query
            System.Data.OleDb.OleDbDataReader reader = command.ExecuteReader();
    
            //this loop will stop until all the rows found have been read
            while (reader.Read())
            {
              //Dont forget to update this section because of the table changes made!!
    
              if (!(reader.IsDBNull(2)))
                this.subject.Text = reader.GetString(2);
              if (!(reader.IsDBNull(3)))
                this.body.Text = reader.GetString(3);//this is where the access db came in really handy!! 
            }
            // always call Close when done reading.
            reader.Close();
          }
        }
      }
    }
    //so everyone this is one way to do what i wanted to do....thanks for the help!

    Loving life since 1981 Preston Lambeth
    • Marked as answer by Ptiwstp1 Tuesday, April 19, 2011 9:25 PM
    Tuesday, April 19, 2011 9:23 PM