Fill in Outlook Template details via VB.net?

Hi,

I am currently using the below code to open an email template with the TO address filled in and the subject field via two textboxes on my form.

I also have a location field on my form, containing a site code for the contact I am sending the email to and a message field. Is it possible for me to place the text from these textboxes into the body of my Outlook template at certain points?

Eg. The template shows:

Location:

Message:

Therefore I am looking to place the text from the message and sitecode text boxes next to these points in my template.

Can anyone show me how to do this?

Many Thanks,
Paul.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Request2 As String
        Dim SendTo2 As String
        Dim location As String
        Dim Message2 As String
 
        Request2 = Request.Text
        SendTo2 = SendTo.Text
        Location = SiteCode.Text
        Message2 = Message.Text
 
 
        Dim myOlApp As Object
        Dim myitem As Object
        myOlApp = CreateObject("Outlook.Application")
        myitem = myOlApp.CreateItemFromTemplate _
            ("c:\templates\contact.msg")
        myitem.Subject = "Customer Contact - <" & Request2 & "> - chkd"
        myitem.To = SendTo2
        myitem.Display()
    End Sub

Open in new window

m0tSiEAsked:
Who is Participating?
 
David LeeCommented:
Hi, mOtSiE.

The simplest solution is to include a token in your template and then use the Replace command to substitute the data for it.  For example, if your template looked like this


Location: /LOC/

Message: /MSG/

Then you could replace the those tokens like this:

myitem.Body = Replace(myitem.Body, "/LOC/", TextBox1.Text)
myitem.Body = Replace(myitem.Body, "/MSG/", TextBox2.Text)

The tokens can be anything you like so long as they are unique.
0
 
m0tSiEAuthor Commented:
Thanks Devil,

I've just tried this and it did add the details in, however it took all of the html from the template out and just left all of the text.

Is there anyway to keep the html?

Thanks,
Paul.
0
 
David LeeCommented:
Use HTMLBody instead of Body
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
m0tSiEAuthor Commented:
Awesome!, You've helped me put it all together in a few hours, compared with all the code i've been trying for the last 2 weeks! :D

Thanks Devil! :D
0
 
m0tSiEAuthor Commented:
Awesome!, You've helped me put it all together in a few hours, compared with all the code i've been trying for the last 2 weeks! :D

Thanks Devil! :D
0
 
David LeeCommented:
You're welcome, Paul.  Glad I could help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.