Sunday, May 25, 2014

Code Formatting Shortcut in Outlook 2010

I often have to write code snippets in my emails and find it such a hassle having to move my mouse to the font selection drop-down to change my font to Consolas every time. I thought that it would be so much easier if I had a keyboard shortcut to switch the font for me (a bit like stackoverflow). After searching around, I found that this can be achieved in Microsoft Outlook by creating a macro that changes the font of your selected text and then assigning a shortcut for that macro.

Creating the macro:
  1. In Outlook, press Alt+F8 to open the Macros dialog
  2. Enter a name for the macro e.g. SetCodeFont and press the Create button
  3. Paste the following macro code into the Visual Basic Editor that opens:
    Sub SetCodeFont()
      Dim objItem As Object
      Dim objInsp As Outlook.Inspector
      Dim objWord As Word.Application
      Dim objDoc As Word.Document
      Dim objSel As Word.Selection
      On Error Resume Next
      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
            objSel.Font.Name = "Consolas"
          End If
        End If
      End If
      Set objItem = Nothing
      Set objWord = Nothing
      Set objSel = Nothing
      Set objInsp = Nothing
    End Sub
  4. On the menu bar of the Visual Basic Editor, click on Tools > References... and tick Microsoft Word 14.0 Object Library
  5. Save (Ctrl+S) and close the Visual Basic Editor
Assigning a keyboard shortcut for the macro:
  1. Open a new mail message
  2. Click on the small drop-down arrow on the Quick Access Toolbar (usually located at the very top of the message window) and select More Commands...
  3. In the Outlook Options dialog that opens, click on the Choose commands from: drop-down list and select Macros
  4. Pick Project1.SetCodeFont and press Add >> to add it to the toolbar
  5. Press OK and you should now see the SetCodeFont macro button appear on the Quick Access Toolbar
Running the macro:

You can run the macro by using Alt+NUM, where NUM is the position of the macro button on the toolbar. For example, if the macro button is the first button on the toolbar, use Alt+1 to run it. Try it out by typing some text in your email message, selecting it and pressing Alt+1 to change the font to Consolas. You can use Ctrl+Space to switch back to the default font.

Note: You may get a security popup when you run the macro, asking you to Allow or Deny access. You can change your security settings by going into File > Options > Trust Center > Trust Center Settings... > Macro Settings, but this will require Administrator privileges (I haven't tried this).


Use Word Macro to Apply Formatting to Outlook Email by Diane Poremsky []

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.