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
A couple notes:
1) I needed to open an existing Word docx. The following worked with Outlook 2013 and Word 2013:
oWord.Documents.Open "C:\Invoices\Sample-invoice-document.docx"
2) I created a self signed cert and added a new button to People ribbon per
https://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/ .
The first time I ran the macro, I was presented with the macro security dialog that has three button options: The first was "Always trust this publisher" (which listed the publisher as "ThisOutlookSession"). The second button was "Enable Macros" and the third was "Disable Macros". Based on a quick research, there are no security problems selecting "Always trust this publisher". Selecting that removes the macro security dialog box for future sessions (ie. close Outlook and save the VBA project if prompted, then restart Outlook to effect).
Just what the doctor ordered. Perfect :-)
Thanks,
Bret