creating a macro to "write" an email

Posted on 2006-05-12
Last Modified: 2010-10-26

I wanted to create a macro in Outlook 2003, but discovered it won't record macros, so I'm a little lost as to what the usual syntax is for Outlook macros (I do a lot of VB in Excel).  

How can I create a macro that will create a new email (not send it, just create and populate) and insert a Word document into the body before the signature and populate the subject line?

Even better, if I have a list of names and email addresses, have outlook create an email for each email address and use the associated names to replace keywords in the Word doc. In this case, I might just want Outlook to go ahead and send the message too.


Question by:ugeb
    LVL 4

    Expert Comment

    to me the right approach for this seems to be to create a script (or excelmacro ) to do this for you , so not from within Outlook.

    If you run a script that uses MAPI to send a mail - it is Outlook that is invoked in the background to create and send your mail.
    So I wouldn't look for this functionality from within Outlook but in a script or vb-project.

    Hope this makes sense....   :-)
    LVL 11

    Author Comment


    Thank you for the response, but the point is I have no idea how to do that.  Regardless of where the macro or VB originates, I have no idea what the commands are or any of that.  That's the crux of my problem and the real question I have.

    Can you give me anything that illustrates what you're describing?
    LVL 76

    Expert Comment

    by:David Lee
    Greetings, ugeb.

    Here's an Outlook macro for creating a new message from code.  It includes setting the subject line and attaching a file.  What you've described in your third paragraph is a mail merge.  There are instructions in both Word and Outlook's online help for doing that.

    Sub NewOutlookMsg()
        Dim olkMessage As Outlook.MailItem
        Set olkMessage = Application.CreateItem(olMailItem)
        With olkMessage
            'Change the subject line text on the next line as desired
            .Subject = "My Subject"
            'Change the file name and path of the file to be attached
            .Attachments.Add "C:\MyFile.Doc"
        End With
    End Sub

    LVL 11

    Author Comment

    Thank you, this definitely gets me a step closer.

    What about inserting a document into the body instead of attaching it?  
    Can I replace the text "XXXXX" with a name or something?

    LVL 76

    Expert Comment

    by:David Lee
    Inserting the contents of a Word document in the body is possible, but I strongly recommend using the built-in mail merge capability instead.  Mail merge includes the ability to replace text with data from an Outlook contact.  If you really want to script your own solution, then the key is making the replaceable text unique.  
    LVL 11

    Author Comment

    I'm not sure if this makes a difference on what you're saying, but I'm not planning on sending out a bunch of emails all at once with the text replacement.

    I get requests for information frequently and I just want to send the same information to each person who requests it when they request it, and personalize it easily.

    Can you give me or point me to code that gets close to this?

    LVL 76

    Accepted Solution

    Ok, the code for this is below.  There may be a simpler way to handle the Word document, I'm not a Word expert.  This works by opening the Word document you want to insert, saving it as a temp file in HTML format, then re-open and read it into the body of the email.  

    Const wdFormatFilteredHTML = 10

    Sub InsertWordDocinMessage()
        Dim olkMsg As Outlook.MailItem, _
            objWordApp As Object, _
            objWordDoc As Object, _
            objFSO As Object, _
            objFile As Object, _
            strPath As String
        'Change the save to path as desired
        strPath = "C:\Temp\"
        Set objWordApp = CreateObject("Word.Application")
        'Change the document path and file name as needed
        Set objWordDoc = objWordApp.Documents.Open("C:\MyFile.doc", , True)
        'Change the temp file name as desired
        objWordDoc.SaveAs strPath & "temp.html", wdFormatFilteredHTML
        objWordDoc.Close False
        Set objWordDoc = Nothing
        Set objWordApp = Nothing
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        'Change the temp file name to match the one above
        Set objFile = objFSO.OpenTextFile(strPath & "temp.html")
        Set olkMsg = Application.CreateItem(olMailItem)
        olkMsg.HTMLBody = objFile.ReadAll
        Set objFile = Nothing
        Set objFSO = Nothing
    End Sub

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
    Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
    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…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    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

    22 Experts available now in Live!

    Get 1:1 Help Now