Open Email in Default Browser


Outlook has the ability to open an email in your browser. This is especially handy with Outlook 2007 and 2010 which does not support the full HTML/CSS specification. Opening a message in the browser allows you to see the message as it would appear if Outlook did support the full HTML/CSS specification. Unfortunately, Outlook always opens the message in Internet Explorer, completely ignoring the default browser setting. It’s a minor annoyance, unless of course you really don’t like IE. There aren’t any settings, or at least none I know of, for altering this behavior. While there isn’t a solution, there is a work-around.

Scripting to the rescue. With a few lines of code we can create a routine that opens a message in the default browser. We can also add the ability to open a message in the browser without having to open it in Outlook first. The solution is appallingly simple. All the code does is determine if a message is already open, or if it should act on the selected message. Once it knows that, it saves the message to your system’s Temp folder, then issues a command to open it in the default browser. This only works for messages, so if you don’t have a message open or selected, then the code displays an error message.

A word of caution about opening messages in the browser. Outlook’s built-in safeguards provide some protection from potentially malicious content in messages. In fact, Microsoft goes so far as to warn you by displaying this dialog-box when you use the built-in View in Browser action. This solution doesn’t provide a warning, although you could add one if you wanted, so please be careful about what you open in the browser.

This solution should work in any version of Outlook.

Adding the Code to Outlook.

  1. Start Outlook
  2. Press ALT+F11 to open the Visual Basic Editor
  3. If not already expanded, expand Microsoft Office Outlook Objects
  4. If not already expanded, expand Modules
  5. Select an existing module (e.g. Module1) by double-clicking on it or create a new module by right-clicking Modules and selecting Insert > Module.
  6. Copy the code from the code snippet box and paste it into the right-hand pane of Outlook’s VB Editor window
  7. Edit the code as needed. I placed a comment where changes can occur.
  8. Click the diskette icon on the toolbar to save the changes
  9. Close the VB Editor
'The declaration MUST appear at the top of the module before any other code
Private Declare Function ShellExecute _
                            Lib "shell32.dll" _
                            Alias "ShellExecuteA" ( _
                            ByVal hwnd As Long, _
                            ByVal lpOperation As String, _
                            ByVal lpFile As String, _
                            ByVal lpParameters As String, _
                            ByVal lpDirectory As String, _
                            ByVal nShowCmd As Long) _
                            As Long

Private Sub OpenInDefaultBrowser()
    Const MACRO_NAME = "Open Message in Default Browser"
    Dim lngRet As Long, strURL As String, olkMsg As Outlook.MailItem
    On Error Resume Next
    Select Case TypeName(Application.ActiveWindow)
        Case "Explorer"
            Set olkMsg = Application.ActiveExplorer.Selection(1)
        Case "Inspector"
            Set olkMsg = Application.ActiveInspector.CurrentItem
    End Select
    If TypeName(olkMsg) <> "Nothing" Then
        If olkMsg.Class = olMail Then
            strURL = Environ("TEMP") & "\" & RemoveIllegalCharacters(olkMsg.Subject) & ".htm"
            olkMsg.SaveAs strURL, olHTML
            lngRet = ShellExecute(0, "open", strURL, 0, 0, 1)
        Else
            msgbox "You can only open an email in the browser.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        msgbox "You do not have an item open or selected.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    On Error GoTo 0
    Set olkMsg = Nothing
End Sub

Function RemoveIllegalCharacters(strValue As String) As String
    ' Purpose: Remove characters that cannot be in a filename from a string.'
    ' Written: 4/24/2009'
    ' Author:  BlueDevilFan'
    ' Outlook: All versions'
    RemoveIllegalCharacters = strValue
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "<", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ">", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, ":", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, Chr(34), "'")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "/", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "\", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "|", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "?", "")
    RemoveIllegalCharacters = Replace(RemoveIllegalCharacters, "*", "")
End Function

Using the Code.

  1. Select or open a message
  2. Run the OpenInDefaultBrowser macro to open the item in the default browser

Running with a single click.

You can add toolbar buttons (Outlook 2003) or QAT buttons (Outlook 2007/2010) to run the macro with a single click. Here’s a link to a page that explains how to do both.

About these ads

12 comments on “Open Email in Default Browser

  1. I performed the steps you indicated (Outlook 2010) but after saving, closing the editor and going back to Outlook, there are still no macros listed to add to the QAT. How do I get this macro saved so that Outlook can see it? Also, even after saving in the VBA editor if I “Run – Macro” there are no macros listed. How do I get this new Macro to show up?

    • You’re the greatest! Just removing that “Private” made it all work great! However, my real reason for the macro didn’t work out. My objective is to print the body of a set of emails (they come in regularly) without the header info (TO:, FROM:, etc.). It appears Outlook 2010 has no way to do that so I read my only option was to open the message in my browser and print from there. But the browser shows the same header info. I don’t want to cut and paste every message to get rid of the header. Have you heard of such a problem. Must be a pretty common complaint about Outlook.

    • I just had a further observation. When using Outlooks “Open in Browser” command it opens in IE (which I never use) and there is no TO/FROM/etc. header information – only the body. But when I use this macro it opens in Firefox as I wanted it to, but it then includes that header stuff again. How do I get rid of the headers? It would be great if I could just print the email body from Outlook 2010 but for some reason they don’t let you do that – it is mandatory to add the header to any printouts.

    • Bill,

      The reason the solution includes the browser is because I used the SaveAs method of the mailitem object. That saves the message just as if you’d done a Save As from the GUI. Saving in that fashion includes the header information. We can get rid of the header easy enough. All we have to do to accomplish that is change how we save the message. Instead of using Save As we need to create a file and write the HTMLBody property of the mailitem object to it. The rest of the code remains the same.

      Here’s the modified version. Replace the OpenInDefaultBrowser sub you have now with the one below. Keep the rest of the code as is.

      Sub OpenInDefaultBrowser()
          Const MACRO_NAME = "Open Message in Default Browser"
          Dim lngRet As Long, strURL As String, olkMsg As Object, objFSO As Object, objFil As Object
          On Error Resume Next
          Select Case TypeName(Application.ActiveWindow)
              Case "Explorer"
                  Set olkMsg = Application.ActiveExplorer.Selection(1)
              Case "Inspector"
                  Set olkMsg = Application.ActiveInspector.CurrentItem
          End Select
          If TypeName(olkMsg) <> "Nothing" Then
              If olkMsg.Class = olMail Then
                  strURL = Environ("TEMP") & "\" & RemoveIllegalCharacters(olkMsg.Subject) & ".htm"
                  Set objFSO = CreateObject("Scripting.FileSystemObject")
                  Set objFil = objFSO.CreateTextFile(strURL)
                  objFil.Write olkMsg.HTMLBody
                  objFil.Close
                  lngRet = ShellExecute(0, "open", strURL, 0, 0, 1)
              Else
                  MsgBox "You can only open an email in the browser.", vbCritical + vbOKOnly, MACRO_NAME
              End If
          Else
              MsgBox "You do not have an item open or selected.", vbCritical + vbOKOnly, MACRO_NAME
          End If
          On Error GoTo 0
          Set olkMsg = Nothing
          Set objFSO = Nothing
          Set objFil = Nothing
      End Sub
      
    • David,
      Thank you so much again. Your modified code worked first time with no problems! And it did just as I expected – opened email body in default browser with no header. So thank you again and to all who read the code is very easy to implement and works great!

      However, it sure seems like there should be some ways out there to print the email body only (with no header) directly from Outlook. Not sure why no one has worked a method for that.

      Anyway, David – thanks again!

    • Bill,

      You’re welcome.

      I can put together a solution that prints the message without the header. I’ll post it when it’s done. It might be a couple of days.

    • Karl,

      Try changing the declaration of the ShellExecute function to

      Private Declare PtrSafe Function ShellExecute _
                                  Lib "shell32.dll" _
                                  Alias "ShellExecuteA" ( _
                                  ByVal hwnd As Long, _
                                  ByVal lpOperation As String, _
                                  ByVal lpFile As String, _
                                  ByVal lpParameters As String, _
                                  ByVal lpDirectory As String, _
                                  ByVal nShowCmd As Long) _
                                  As Long
      

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