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 |
---|---|
Appointments | olAppointment |
Contacts | olContact |
Journal | olJournal |
Message | olMail |
Note (sticky note) | olNote |
Post | olPost |
Tasks | olTask |
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
Hi Diane, I think the code on this page will resolve my issue, IF I can get it to run. I want to change all of my Outlook 2010 Contacts' Note Field properties as follows. Font: Arial, Size: 10pt, Color: Black and remove all formatting spaces, indents etc. I can do this individually but it will take a ton of time. I want to apply these settings to my existing 600+ contacts or set these as the default for all new contacts, then I would import my contacts from a CSV file. I'm NOT a VBA expert. I've tried following the directions in this post but I can't get any of the VBA code to run without errors. I did follow your directions on "How to use Outlook's VBA Editor". I get VBA running, paste your code under "Change the formatting of selected items" and hit Run. I immediately get an error in VBA. Please see attached JPG file. I've tried a few of the VBA codes on this page and all error. Can you please tell me what I am doing wrong with these VBA codes? What is causing the errors when I try to run the code? Also… Read more Âğ
Help! I am trying to create a macro to change font size for a notes in a open MS outlook contact. Here is what I have that produces an error (how do I fix the last 2 lines?) Thanks!
Sub Clean_notes()
Set mymessage = ActiveInspector.CurrentItem
mymessage.Body = Replace(mymessage.Body, vbCrLf & vbCrLf, vbCrLf)
mymessage.Body = Replace(mymessage.Body, vbTab, " ")
mymessage.Body.Font.Name = "Arial" '<-this is wrong
mymessage.Body.Font.Size = "9"
End Sub
Outlook doesn't support body.font directly - you need to use the word object model.
Yeah! This works! Just had to set Microsoft Word Object Library in Tools, References as noted in the main article. Thank you for the help, Diane. -D
Diane, thanks for this resource. I've used several of your code items suggestions in the past. I recently exported all my contacts to excel ... made a bunch of changes to them (found it easier to do the mass changes that way) and then loaded them back up. Unfortunately, the formatting for the "Notes" area of the contact has changed for some reason. I need to have some code that goes through all the records and set just the "Notes" area to a set font type, size and default black color. I tried going through the code you posted in this area but I couldn't piece it all together. Can you help?
Sorry it has taken so long to get to this - i took a weekend off and every just snowballed. :( Ether the second or third macro should work for this. change the font name and size to the one you want to use.
With objSel.Font
.Name = "Arial"
.Size = 12
.Color = wdColorBlack
End With
Thank you so much Diane for sharing your knowledge. I would have never been able to get started without your helpful instruction. Regarding changing the appearance of the text in the notes field of Outlook contacts, I've got the above working but would also have some contacts that have mysteriously taken on the Heading 1 style and I'd like to change them all to Normal or No Spacing. Could you possibly point me in the right direction?
Thank you very very much.
Cheers, ...Alex
use this line to set the style - you replace no spacing with astyle name.
objSel.Style = objDoc.Styles("No Spacing")
this line removes all formatting
objSel.ClearFormatting
Either would replace the With objsel.font block in the code sample.
Thank you, thank you, thank you! With your suggestion I also needed to rem out "End With". When I run it on my entire contact list it seems to skip some so I'll run it on a few hundred contacts at at time. This puts to rest an issue I've been trying to figure out for quite some time. Again, thank you so very much for teaching non-programmer types how to automate such things using VBA!
Sorry this took me a while to get back to because I had to find time to learn a bit more about VB to understand your question. It's working but raises two more related questions: is there coding to keep the color of the links in the notes blue? And is there a way to show the original display text rather than the actual link?
When I run the code the notes section font looks great. I just find all of the hyperlinks are now black as well. Thoughts? Ideas?
I love that these solutions are available, Diane. I have tried running the first macro and get and complie error: Block if without End if which then highlights the line above in the code called: Private Sub m_Inspector_Activate ().
Perhaps I am missing a step or have done something wrong? I am trying to clean up the changed font in my Contacts' Notes fields which I think is happening when I use iCloud to sync with my iPhone. They are appearing as Calibri again even when I have individually set them to Arial 8. Very odd to me.
Did you click in the application startup macro and press run or restart outlook?