Create a Letter to a Contact using VBA

Last reviewed on October 3, 2012

Outlook used to have a "new letter to contact" command but it was removed from Outlook, beginning with Office 2007. With this feature gone, the recommend method of using Outlook data in Word is mail merge. However mail merge is slow. Very slow. I know, because I was using it when I created invoices. I wanted a faster way to address the invoices and after creating the Create a deferred birthday message to a Contact, decided that this was the way to do it.

This code sample takes the name and address from a contact and inserts them into a Word template's bookmarks.

Most 'use Outlook contacts in Word' code samples on the Internet call Outlook from Word and load all of the contacts into a dialog box. I *know* who I need to send the Invoice to, I don't want to list all addresses then pick the right one. I can do that now without a macro, either using Mail Merge or the Address book command. I wanted to pick the contact in Outlook and send it to Word. I also wanted a solution that worked with any Contact folder, including linked SharePoint Contacts or BCM Contacts.

A macro in my Word template adds an invoice # and saves the document as a text file. The Total fields are calculated fields: enter the subtotal and a discount (if any) then select the tables and press F9 to update the fields. (My own invoice saves a *.docx to a SharePoint site and a *.pdf to my hard drive, which I send to the customer.)

I use two CompanyName bookmarks in the Word template. One called CompanyName wrapped inside a bookmark called Company. This allows me to use the Company name in the filename too.

Download the sample template and add this macro to Outlook's VBA Editor, changing the path to the Word template.

To use this code with your own template, add bookmarks to a word template where you want the person's name, company name, and mailing address to be inserted. Don't forget to change the path to your template! You can use any Contact field: birthday, email address, phone number, etc; all you need to do is add a bookmark to the Word template and edit the macro to add the two .Selection lines (containing the correct Outlook field name) for each bookmark.

Send Contact Address to Word Code Sample

You need to set a Reference to the Word object model in the VB Editor's Tools, References. If not, you'll get a Compile error: User-defined type not defined.

To use: Select a contact and run the macro. This code does not work with an open Contact; you need to select the Contact, not open it.

Public Sub SendAddressToWord()
'Dim oContact
If TypeName(ActiveExplorer.Selection.Item(1)) = "ContactItem" Then
 Set oContact = ActiveExplorer.Selection.Item(1)
 
   ' Declare the variable.
   Dim oWord As Word.Application

    ' Set the variable (runs new instance of Word).
    Set oWord = CreateObject("Word.Application")

    oWord.Documents.Add Template:="C:\Invoices\Sample-invoice-template.dotm"

   With oWord

' Find the bookmark, insert an Outlook field
    .Selection.GoTo What:=wdGoToBookmark, Name:="FullName"
    .Selection.TypeText Text:=oContact.FirstName & " " & oContact.LastName
        
    .Selection.GoTo What:=wdGoToBookmark, Name:="CompanyName"
     .Selection.TypeText Text:=oContact.CompanyName
        
     .Selection.GoTo What:=wdGoToBookmark, Name:="MailingAddress"
     .Selection.TypeText Text:=oContact.MailingAddress
     
' Show the Word document
     .Visible = True
   End With


   ' Clear the variable from memory.
    Set oWord = Nothing

Else
MsgBox "Sorry, you need to select a contact"
End If

End Sub


Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.