A user in the Microsoft Answers forum wanted to know how to use a Word macro to apply formatting to selected text in Outlook 2010.
Outlook does not (and never had) a macro recorder but you can use some VBA code that was recorded in Word, in Outlook macros provided you reference the Word object model, and set the Word object and selection (as seen in the code below). You'll need to set the reference in the VB Editor's Tools, References menu. You'll also need to have macro security set to low or or sign the macro with a certificate to use it.
See How to use Outlook’s VBA Editor for help using the editor, setting security levels, and signing macros.
I tested this macro in Outlook 2010 and Outlook 2013; it should also work in Outlook 2007.
Format Selected Text Macro
To use, open the VBA Editor (Alt+F11) and paste the code into a module. Select a block of text while composing a message and run the macro.
Public Sub FormatSelectedText() Dim objItem As Object Dim objInsp As Outlook.Inspector ' Add reference to Word library ' in VBA Editor, Tools, References Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection On Error Resume Next 'Reference the current Outlook item Set objItem = Application.ActiveInspector.currentItem If Not objItem Is Nothing Then If objItem.Class = olMail Then Set objInsp = objItem.GetInspector If objInsp.EditorType = olEditorWord Then Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection ' replace the With block with your code With objSel ' Formatting code goes here .Font.Color = wdColorBlue .Font.Size = 18 .Font.Bold = True .Font.Italic = True .Font.Name = "Arial" End With End If End If End If Set objItem = Nothing Set objWord = Nothing Set objSel = Nothing Set objInsp = Nothing End Sub
Find and Format Text Code Sample
This example creates appointments for the selected contact(s), adds their name and address to the appointment body then changes the font used for their name and address to 14 point bold. This method can be used with any word or phrase stored in a variable.
The original macro this code sample came from collects data from all selected contacts and creates a string to use in a single appointment but I simplified it for this example. The original macro is at Outlook 2007 Calendar.
Sub CreateAppointmentSelectedContact() Dim ObjItem As Object Dim strFullName As String Dim strPhone As String Dim strAddress As String Dim strDynamicDL2 As String Dim strDynamicDL3 As String Dim StartDateTime Dim itmAppt Set oContact = ObjItem Set objApp = CreateObject("Outlook.Application") Set objNS = objApp.GetNamespace("MAPI") Set objSelection = objApp.ActiveExplorer.Selection For Each ObjItem In objSelection If ObjItem.Class = olContact Then strFullName = ObjItem.FullName strPhone = ObjItem.HomeTelephoneNumber strAddress = ObjItem.HomeAddressStreet & ", " & ObjItem.HomeAddressCity & ", " & ObjItem.HomeAddressState & " " & ObjItem.HomeAddressPostalCode strDynamicDL2 = ("Name: ") & strFullName strDynamicDL3 = ("Address: ") & strAddress Set MyFolder = Session.GetDefaultFolder(9) Set itmAppt = MyFolder.Items.Add("IPM.Appointment") itmAppt.Subject = strFullName & (" -- ") & strPhone With itmAppt .Body = strDynamicDL2 & vbCrLf & strDynamicDL3 End With StartDateTime = Date + 3.5 itmAppt.Start = StartDateTime End If Next itmAppt.Display Dim objInsp As Outlook.Inspector Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection Set objInsp = itmAppt.GetInspector Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection objSel.Find.ClearFormatting objSel.Find.Replacement.ClearFormatting With objSel.Find.Replacement.Font .Size = 14 .Bold = True .Underline = wdUnderlineSingle .Color = wdColorBlack End With With objSel.Find .Text = strFullName .Replacement.Text = strFullName .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With objSel.Find.Execute Replace:=wdReplaceAll With objSel.Find .Text = strAddress .Replacement.Text = strAddress .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With objSel.Find.Execute Replace:=wdReplaceAll Set objInsp = Nothing Set objDoc = Nothing Set objSel = Nothing Set objMsg = Nothing Set objMsg = Nothing Set ObjItem = Nothing Set objFolder = Nothing Set objNS = Nothing Set objApp = Nothing End Sub
How to Use Macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 and above, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
- Set a reference to the Word Object Model in the VBA editor's Tools, References dialog.
More information as well as screenshots are at How to use the VBA Editor