Use a Word Macro in Outlook

Outlook doesn't have a macro recorder, however, you can use many of the macros recorded in Word in Outlook, too.

Last modified: 2012/09/06 | Accessed: 20.053  | #95
◀ Previous sample Next sample ▶
Category-Manager Category-Manager
Access the master category list in the blink of an eye, share your color categories in a network, get a reminder service, and more.

Since the version 2007, Outlook always uses Microsoft Word as e-mail editor. That allows developers to use the powerful library of Word to edit the content of e-mails and other items. Even better, there is a macro recorder in Word, which you don't have in Outlook. That is you can record a macro in Word and use the same code in the Outlook VBA project, too.

You can record a macro via the Developer tab in Word. If you don't see it on the ribbon, right click on the ribbon, choose Customize the Ribbon. In the list right hand check Developer and OK out. On the Developer tab click Record Macro.

In this sample I've started the recording, then typed the word "Hello" into the document and marked it bold. The following shows the result of the macro recorder:

tip  How to add macros to Outlook
Sub Macro1()
' Macro1 Macro
    Selection.TypeText Text:="Hello"
    Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
    Selection.Font.Bold = wdToggle
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

As you see Word uses the Selection object to edit the text of a document. In order to use such a macro in Outlook, just set a variable to the Selection object of the email.

(First click Tools/References, and set a reference to the "Microsoft Word x.0 Object Library" so Outlook can recognize the objects used by Word.)

Public Sub UseWord()
  Dim Ins As Outlook.Inspector
  Dim Document As Word.Document
  Dim Word As Word.Application
  Dim Selection As Word.Selection

  Set Ins = Application.ActiveInspector
  Set Document = Ins.WordEditor
  Set Word = Document.Application
  Set Selection = Word.Selection

  'insert here your macro from word

End Sub
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message