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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
David LeeCommented:
Use HTMLBody instead of Body
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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
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
David LeeCommented:
You're welcome, Paul.  Glad I could help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.