Fill in Outlook Template details via

Posted on 2008-11-14
Last Modified: 2012-05-05

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:



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,
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 _


        myitem.Subject = "Customer Contact - <" & Request2 & "> - chkd"

        myitem.To = SendTo2


    End Sub

Open in new window

Question by:m0tSiE
    LVL 76

    Accepted Solution

    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.

    Author Comment

    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?

    LVL 76

    Expert Comment

    by:David Lee
    Use HTMLBody instead of Body

    Author Comment

    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

    Author Closing Comment

    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
    LVL 76

    Expert Comment

    by:David Lee
    You're welcome, Paul.  Glad I could help.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now