VBOffice

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: 73.503  | #95
◀ Previous sample Next sample ▶
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.

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
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.

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
ReplyAll ReplyAll
ReplyAll alerts you before unintentionally replying all, or if you are a confidential BCC recipient of the e-mail.
email  Send a message