Today's Lazy Programmer provides an answer for Martin, who asked how to change the font in the Notes field only in Contacts, but not in the other Outlook items types. He also wants to change the font used with existing Contact items.
When you change the font in Options > Mail > Stationery and Fonts, the change is applied to all new Outlook items. Martin wants to use a one font for contacts and another for his email and other Outlook items. (Sticky notes have their own font settings and don't use Word as the editor.)
One option is to use custom stationery for your email messages. While this works fine if you want to use one font for everything except email, since Outlook now uses Word as the email editor, you can use Word VBA to change the font in the notes field in any new item type except email and sticky notes. This code sample shows you how.
This works on new items only, it will not change the font in existing items.
I'm starting with Michael Bauer's Inspector: Edit new items code sample and adding a few lines to set the font. Now when I open any new Outlook item (but not a new message or sticky note), the body font is changed. In my example (so we can easily see that it works), I'm using Wingdings (and size 18 font). Replace it with a font name as seen in the Font selector. (You can use lower case names as the font name is not case-sensitive.)
This is an Application_Startup macro. Paste it into ThisOutlookSession and set a reference to the Microsoft Word Object Library in Tools, References. To test it without restarting Outlook you'll need to click in Application_Startup and click the Run button. Oh, and macro security needs to be set to Low during testing and the macro signed once you're satisfied with it. How to use Outlook’s VBA Editor has more information if you are new to VBA.
Private WithEvents m_Inspectors As Outlook.Inspectors Private WithEvents m_Inspector As Outlook.Inspector '//slipstick.me/888jf Private Sub Application_Startup() Set m_Inspectors = Application.Inspectors End Sub Private Sub m_Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector) Set m_Inspector = Inspector End Sub ' Set a reference to Word object model in ' Tools, References Private Sub m_Inspector_Activate() ' Sticky notes don't use Word editor, so we need check for Notes too If m_Inspector.CurrentItem.Class = olMail Or m_Inspector.CurrentItem.Class = olNote Then Exit Sub Else Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection With olSelection.Font .Name = "wingdings" .Size = 18 End With ' Delete this line after testing olSelection.InsertBefore olSelection.Font.Name & " your sample text" Set m_Inspector = Nothing End Sub
Change the font for only one item type
If, like Martin, you only want to change the font for one Outlook item type, the If... then statement looks for the item type and changes the font. Other types are not touched.
Replace the m_Inspector_Activate() sub with the following code:
Private Sub m_Inspector_Activate() '//slipstick.me/888jf If m_Inspector.CurrentItem.Class = olContact Then Dim olInspector As Outlook.Inspector Dim olDocument As Word.Document Dim olSelection As Word.Selection Set olInspector = Application.ActiveInspector() Set olDocument = olInspector.WordEditor Set olSelection = olDocument.Application.Selection With olSelection.Font .Name = "wingdings" .Size = 18 End With ' Used during testing olSelection.InsertBefore olSelection.Font.Name & " your sample text" End If Set m_Inspector = Nothing End Sub
|New item type||Object class name|
|Note (sticky note)||olNote|
Change the formatting of selected items
This code changes the font in the body of selected appointment, contact, or task items. To use, select the items (it's easier to use a list view, especially for appointments) then run the code. Each item will quickly open then close as Outlook makes the changes.
I recommend selecting some items, then Ctrl+C, V to create a copy of the items for testing.
Put this macro in a module.
Public Sub ChangeFormatting() 'http://slipstick.me/888jf Dim currentExplorer As Explorer Dim Selection As Selection Dim obj As Object Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection On Error Resume Next Set currentExplorer = Application.ActiveExplorer Set Selection = currentExplorer.Selection For Each obj In Selection Set objItem = obj objItem.Display Set objInsp = objItem.GetInspector Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection objSel.WholeStory With objSel.Font .Name = "broadway" .Size = 12 .Color = wdColorGreen End With objItem.Close olSave Err.Clear Next Set currentExplorer = Nothing Set obj = Nothing Set Selection = Nothing End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look 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.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
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.
To put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
Set a reference to other Object Libraries
If you receive a "User-defined type not defined" error, you need to set a reference to another object library.
- Go to Tools, References menu.
- Locate the object library in the list and add a check mark to it. (Word and Excel object libraries version numbers will match Outlook's version number.)
More information as well as screenshots are at How to use the VBA Editor