• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 777
  • Last Modified:

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

0
m0tSiE
Asked:
m0tSiE
  • 3
  • 3
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now