Swap That Font!


Twitter is a veritable fountain of ideas and challenges. Here’s an example.

On reading this I thought Alex was talking about Outlook notes since they use the Comic Sans MS font by default. I responded to Alex’s tweet telling him that he can change the font to anything he wants. I was surprised when Alex replied saying that he wants to change the font on incoming messages. I would never have thought of doing that. Then again I don’t have anyone sending me messages written in Comic Sans MS. Maybe I’d have thought of doing this if I did.

Outlook does not have a built-in ability to change the font on received messages. I googled to see if anyone else had tackled this issue and found a couple of items. The solution they proposed calls for switching the message format from HTML to plain text and back. Doing this changes the message to the default font, but it also eliminates formatting, images, and anything else that’s not supported in plain text messages. In my opinion that makes the solution useless. Time to come up with a better approach.

The solution I settled on uses scripting to search a message’s raw HTML and replace all instances of one font name with another. The code comes in two parts: a class that does the actual work, and code that creates an instance of the class. The script traps inbound messages via Outlook’s NewMailEx event. This event fires each time new items arrive. The code checks each new item to see if it’s an email. If it is, then the code replaces all instances of one font name with another. For example, Alex can tell the script to replace all instances of “Comic Sans MS” with “Arial”. The actual search and replace uses a regular expressions (RegExp) object, which results in a very fast replacement operation. All that’s replaced is the font. This solution cannot make changes to the font size, style, etc. It’s a simple replacement of one font name with another. While Alex wants to eliminate Comic Sans MS, the code can replace any font with another. Of course you do have to know the actual name of the font. You can get that by looking at the list of fonts either in Outlook or in Windows. The replacement operation is not case sensitive, so you can enter it as “Comic Sans MS” or “comic sans ms”. Keep in mind that replacement operation will replace ALL references to the font name. Say that Alex tells the code to replace “Comic Sans MS” with “Arial”. Mary sends Alex a message telling him “The new publication will use Comic Sans MS for …”. When Alex reads the message he’s going to see “The new publication will use Arial for …” I doubt there are that many messages talking about font names, but you do need to remember that the script will replace them if there’s a match.

Instructions and Code.

The code comes in two parts. This is part 1. Follow these instructions to add the code.

  1. Start Outlook
  2. Press ALT+F11 to open the VB editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. Right-click on Class Modules, select Insert > Class Module
  5. In the Properties panel click on Name and enter clsFontReplacement
  6. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Click the diskette icon on the toolbar to save the changes
Private WithEvents olkApp As Outlook.Application
Private objReg As Object, strReplacementFont As String

Private Sub Class_Initialize()
    Set objReg = CreateObject("VBScript.RegExp")
    With objReg
        .Global = True
        .IgnoreCase = True
    End With
    Set olkApp = Outlook.Application
End Sub

Private Sub Class_Terminate()
    Set olkApp = Nothing
    Set objReg = Nothing
End Sub

Private Sub olkApp_NewMailEx(ByVal EntryIDCollection As String)
    Dim arrEID As Variant, strEID As Variant, objItm As Object
    arrEID = Split(EntryIDCollection, ",")
    For Each strEID In arrEID
        Set objItm = Session.GetItemFromID(strEID)
        If objItm.Class = olMail Then
            ReplaceFont objItm
        End If
    Next
End Sub

Private Sub ReplaceFont(olkMsg As Outlook.MailItem)
    If (objReg.Pattern <> "") And (strReplacementFont <> "") Then
        olkMsg.HTMLBody = objReg.Replace(olkMsg.HTMLBody, strReplacementFont)
        olkMsg.Save
    End If
End Sub

Public Property Get OriginalFont() As String
    OriginalFont = objReg.Pattern
End Property

Public Property Let OriginalFont(ByVal strValue As String)
    objReg.Pattern = strValue
End Property

Public Property Get ReplacementFont() As String
    ReplacementFont = strReplacementFont
End Property

Public Property Let ReplacementFont(ByVal strValue As String)
    strReplacementFont = strValue
End Property

Outlook 2007.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Edit the code as desired. I’ve included a comment where you can make changes.
  4. Click the diskette icon on the toolbar to save the changes
  5. Close the VB Editor
  6. Click Tools > Trust Center
  7. Click Macro Security
  8. Set Macro Security to “Warnings for all macros”
  9. Click OK
  10. Close Outlook
  11. Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.

Outlook 2010.

  1. If not already expanded, expand Microsoft Office Outlook Objects and click on ThisOutlookSession
  2. Copy the code from the Code Snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  3. Edit the code as desired. I’ve included a comment where you can make changes.
  4. Click the diskette icon on the toolbar to save the changes
  5. Close the VB Editor
  6. Click File and select Options
  7. When the Outlook Options dialog appears click Trust Center then click the Trust Center Settings button
  8. Click Macro Settings
  9. Select either of the two bottom settings (i.e. “Notifications for all macros” or “Enable all macros (not recommended; potentially dangerous code can run)”. The choice of which to choose is up to you. If you select “Notifications”, then you’ll be prompted at times to enable macros. If you pick “Enable all” then there’s a chance that a malicious macro could run. It’s a question of how much risk you want to assume.
  10. Click Ok until the dialog-boxes have all closed
  11. Close Outlook
  12. Start Outlook. Outlook will display a dialog-box warning that ThisOutlookSession contains macros and asking if you want to allow them to run. Say yes.
Private objAC As clsApptCategorizer
Private objFontReplacement As clsFontReplacement

Private Sub Application_Quit()
    Set objAC = Nothing
    Set objFontReplacement = Nothing
End Sub

Private Sub Application_Startup()
    Set objAC = New clsApptCategorizer
    Set objFontReplacement = New clsFontReplacement
    With objFontReplacement
        'On the next line change the name of the font.  This is the font you want to eliminate.
        .OriginalFont = "Comic Sans MS"
        'On the next line change the name of the font.  This is the font that will replace the font you are eliminating.
        .ReplacementFont = "Tahoma"
    End With
End Sub
Advertisements

2 comments on “Swap That Font!

  1. Great tip. Made one small adjustmnet to leave Comic Sans MS in the body of the email and just replace the font.

    .OriginalFont = “font-family:” & Chr(34) & “Comic Sans MS” & Chr(34)
    .ReplacementFont = “font-family:” & Chr(34) & “Tahoma” & Chr(34)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s