Print An Email Without The Header


 

Bill, a visitor to this blog, asked if I knew of a way to print an email without printing the header (i.e. From, Sent, To, and Subject) at the top of the message. In Bill’s words

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.

I’m not aware of a built-in way to do this, so I created a way. The macro below prints the current message without the header. It does this by copying the message body into a blank Internet Explorer window, then calling IE’s built-in print command. The output goes to the default printer, so Bill will need to make sure he has the right printer set as default before invoking the macro.

Instructions.

Follow these instructions to add 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 InsertModule.
  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
  8. Close the VB Editor
Sub PrintEmailWithoutTheHeader()
    Const OLECMDID_PRINT = 6
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2
    Const MACRO_NAME = "Print Email Without The Header"
    Dim olkMsg As Object, objIE As Object
    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
            Set objIE = CreateObject("InternetExplorer.Application")
            With objIE
                .Navigate2 "about:blank"
                Do Until .readyState = 4
                    DoEvents
                Loop
                .document.Title = olkMsg.Subject
                .document.Body.innerHTML = olkMsg.HTMLBody
                .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
            End With
        Else
            MsgBox "This macro can only print an email.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "You do not have an item open or selected.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    objIE.Quit
    Set olkMsg = Nothing
    Set objIE = Nothing
End Sub 

Using the Solution.

  1. Open or select a message.
  2. Run the macro.

Adding Buttons to Run the Macro with a Single Click.

If Bill wants to run the macro with a single click, then he’ll need to add a toolbar button in Outlook 2007 or a button on the Quick Access Toolbar (QAT) for Outlook 2010.

Outlook 2007. Follow these instructions to add a toolbar button that runs the macro.

Outlook 2010. Follow these instructions to add the macro to the QAT.

Revisions.

.

1                  

I put this revision together in response to a comment from a reader named Tech2. Tech2 asked if there was some way to suppress the page header and footer that Internet Explorer adds to the printout. To accomplish that, I added code that reads the header and footer settings from the registry, stores them, turns the settings off for this print, then restores them to their original values once the message has finished printing. Tech2 will use the instructions from the original post to add this version of the code to Outlook.

Sub PrintEmailWithoutTheHeader()
    Const OLECMDID_PRINT = 6
    Const OLECMDEXECOPT_DONTPROMPTUSER = 2
    Const MACRO_NAME = "Print Email Without The Header"
    Dim olkMsg As Object, objIE As Object, strHead As String, strFoot As String
    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
            strHead = GetIEHeaderFooter("header")
            strFoot = GetIEHeaderFooter("footer")
            SetIEHeaderFooter "header", ""
            SetIEHeaderFooter "footer", ""
            Set objIE = CreateObject("InternetExplorer.Application")
            With objIE
                .Navigate2 "about:blank"
                Do Until .readyState = 4
                    DoEvents
                Loop
                .document.Title = olkMsg.Subject
                .document.Body.innerHTML = olkMsg.HTMLBody
                .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
            End With
            SetIEHeaderFooter "header", strHead
            SetIEHeaderFooter "footer", strFoot
        Else
            MsgBox "This macro can only print an email.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
        End If
    Else
        MsgBox "You do not have an item open or selected.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
    End If
    objIE.Quit
    Set olkMsg = Nothing
    Set objIE = Nothing
End Sub

Function GetIEHeaderFooter(strType As String) As String
    Dim objShl As Object
    Select Case LCase(strType)
        Case "footer", "header"
            Set objShl = CreateObject("WScript.Shell")
            GetIEHeaderFooter = objShl.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\" & LCase(strType))
    End Select
    Set objShl = Nothing
End Function

Sub SetIEHeaderFooter(strType As String, strVal As String)
    Dim objShl As Object
    Select Case LCase(strType)
        Case "footer", "header"
            Set objShl = CreateObject("WScript.Shell")
            objShl.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\" & LCase(strType), strVal, "REG_SZ"
    End Select
    Set objShl = Nothing
End Sub
Advertisements

166 comments on “Print An Email Without The Header

  1. Good morning, your script has been of great help to me. I would need two changes for a particular application. Can I write you somewhere in private?

    • Hi, Siobhan.

      Thanks!

      I hadn’t thought about printing multiple messages at once, so the original version can only print one at a time. The code below will allow you to print multiple messages all at once. Replace the original code with the version below. To print multiple messages, select all the messages you want to print and then run the PrintEmailWithoutTheHeader macro.

      Sub PrintEmailWithoutTheHeader()
          Const MACRO_NAME = "Print Email Without The Header"
          Dim olkMsg As Object
          Select Case TypeName(Application.ActiveWindow)
              Case "Explorer"
                  For Each olkMsg in Application.ActiveExplorer.Selection
                      PrintEmail olkMsg
                  Next
              Case "Inspector"
                  PrintEmail Application.ActiveInspector.CurrentItem
          End Select
          Set olkMsg = Nothing
      End Sub
      
      Sub PrintEmail(olkMsg As Object)
          Const OLECMDID_PRINT = 6
          Const OLECMDEXECOPT_DONTPROMPTUSER = 2
          Const MACRO_NAME = "Print Email Without The Header"
          Dim objIE As Object
          If TypeName(olkMsg) <> "Nothing" Then
              If olkMsg.Class = olMail Then
                  Set objIE = CreateObject("InternetExplorer.Application")
                  With objIE
                      .Navigate2 "about:blank"
                      Do Until .readyState = 4
                          DoEvents
                      Loop
                      .document.Title = olkMsg.Subject
                      .document.Body.innerHTML = olkMsg.HTMLBody
                      .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
                  End With
              Else
                  MsgBox "This macro can only print an email.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
              End If
          Else
              MsgBox "You do not have an item open or selected.  Operation cancelled.", vbCritical + vbOKOnly, MACRO_NAME
          End If
          objIE.Quit
          Set objIE = Nothing
      End Sub
      
  2. hello David,
    i have this one, and it runs perfectly, but it doesn’t show on the script list so i can select it to make a rule.
    please help me.

    Sub PrintEmailWithoutTheHeader()
    Const MACRO_NAME = “Print Email Without The Header”
    Const wdMove = 0
    Const wdPrintFromTo = 3
    Const wdStory = 6
    Dim olkMsg As Object, wrdApp As Object, wrdDoc As Object, olkDoc As Object
    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
    Set olkDoc = olkMsg.GetInspector.WordEditor
    olkDoc.Range.Select
    olkDoc.Range.Copy
    Set wrdApp = CreateObject(“Word.Application”)
    Set wrdDoc = wrdApp.Documents.Add
    wrdApp.Selection.EndKey wdStory, wdMove
    wrdApp.Selection.Paste
    wrdDoc.PrintOut Background:=False, Range:=wdPrintFromTo, From:=”1″, To:=”1″
    Else
    MsgBox “This macro can only print an email. Operation cancelled.”, vbCritical + vbOKOnly, MACRO_NAME
    End If
    Else
    MsgBox “You do not have an item open or selected. Operation cancelled.”, vbCritical + vbOKOnly, MACRO_NAME
    End If
    wrdDoc.Close SaveChanges:=False
    wrdApp.Quit SaveChanges:=False
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
    Set olkMsg = Nothing
    End Sub

    • Hi, petroni417.

      The code in the original post isn’t designed to run from a rule. Use this version instead.

      Sub PrintEmailWithoutTheHeader(olkMsg As Outlook.MailItem)
          Const OLECMDID_PRINT = 6
          Const OLECMDEXECOPT_DONTPROMPTUSER = 2
          Dim objIE As Object
          Set objIE = CreateObject("InternetExplorer.Application")
          With objIE
              .Navigate2 "about:blank"
              Do Until .ReadyState = 4
                  DoEvents
              Loop
              .Document.title = olkMsg.Subject
              .Document.Body.innerHTML = olkMsg.HTMLBody
              .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
          End With
          objIE.Quit
          Set objIE = Nothing
      End Sub
      
    • When i recive a new email, outlook pops up an error message saying “You do not have an item open or selected. Operation cancelled.”
      Then it opens MVB with “Run-Time error ’91′” and asks me to end or debug.
      When i click debug, it has highlighted with yellow the first line o this :
      wrdDoc.Close SaveChanges:=False
      wrdApp.Quit SaveChanges:=False
      Set wrdDoc = Nothing
      Set wrdApp = Nothing
      Set olkMsg = Nothing
      End Sub

    • Hi, petroni417.

      Did you replace the original code with the modified version? The error sounds like you’re still using the original code.

    • I replaced it with the one you gave me, but it prints also the subject on header and date on the footer. I want it to print only the body. And from what i read in this article, the way is to print it via word. But clearly i’m doing something wrong.

    • Hi, petroni417.

      Suppressing the header and footer doesn’t require using Word. The version below does that using Internet Explorer. Remove all the code you have now and use the version below.

      Sub PrintEmailWithoutTheHeader(olkMsg As Outlook.MailItem)
          Const OLECMDID_PRINT = 6
          Const OLECMDEXECOPT_DONTPROMPTUSER = 2
          Const MACRO_NAME = "Print Email Without The Header"
          Dim objIE As Object, strHead As String, strFoot As String
          strHead = GetIEHeaderFooter("header")
          strFoot = GetIEHeaderFooter("footer")
          SetIEHeaderFooter "header", ""
          SetIEHeaderFooter "footer", ""
          Set objIE = CreateObject("InternetExplorer.Application")
          With objIE
              .Navigate2 "about:blank"
              Do Until .readyState = 4
                  DoEvents
              Loop
              .document.Title = olkMsg.Subject
              .document.Body.innerHTML = olkMsg.HTMLBody
              .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
          End With
          SetIEHeaderFooter "header", strHead
          SetIEHeaderFooter "footer", strFoot
          objIE.Quit
          Set olkMsg = Nothing
          Set objIE = Nothing
      End Sub
       
      Function GetIEHeaderFooter(strType As String) As String
          Dim objShl As Object
          Select Case LCase(strType)
              Case "footer", "header"
                  Set objShl = CreateObject("WScript.Shell")
                  GetIEHeaderFooter = objShl.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\" & LCase(strType))
          End Select
          Set objShl = Nothing
      End Function
       
      Sub SetIEHeaderFooter(strType As String, strVal As String)
          Dim objShl As Object
          Select Case LCase(strType)
              Case "footer", "header"
                  Set objShl = CreateObject("WScript.Shell")
                  objShl.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\" & LCase(strType), strVal, "REG_SZ"
          End Select
          Set objShl = Nothing
      End Sub
      
    • Hello again David !
      I want to ask you if it is posible to make that work on windows xp home, with office 2010, and internet explorer version 6.0.2900.5512.xpsp.080413-211 ?

    • Hi, petroni417.

      The solution will work on XP and will work with Office 2010. I can’t tell you if it will work with IE6 or not. I don’t have a system with IE6, so no way to test and I don’t remember whether IE6 supports the IE-specific actions in that solution. My suggestion is to try it and see what happens. At worst it won’t work.

    • Hi, petronie417.

      I don’t think I ever posted a version that uses Word. You posted one in your comment on 1/14/2016 at 8:09 PM. I replied back on 4/18/2016 at 5:52 AM saying that “Suppressing the header and footer doesn’t require using Word.”

  3. thank you for the solution i wanted to know if there is a way to create a rule so that it runs automatically when email is received?

    Cheers!!

    • Hi, Ash.

      Sure. You’ll need to use this code instead of the code in the post. Once you’ve added this code to Outlook, create a rule that fires for the messages you want to print. Set that rule’s action to “run a script” and select this script as the one to run.

      Sub PrintEmailWithoutTheHeader(olkMsg As Outlook.MailItem)
          Const OLECMDID_PRINT = 6
          Const OLECMDEXECOPT_DONTPROMPTUSER = 2
          Dim objIE As Object
          Set objIE = CreateObject("InternetExplorer.Application")
          With objIE
              .Navigate2 "about:blank"
              Do Until .ReadyState = 4
                  DoEvents
              Loop
              .Document.title = olkMsg.Subject
              .Document.Body.innerHTML = olkMsg.HTMLBody
              .ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, 2, vbNull
          End With
          objIE.Quit
          Set objIE = Nothing
      End Sub
      
    • Hi David! you sent me the single line to add if we wanted to print 2 copies ( Const COPIES_TO_PRINT = 2 ). But the example you sent was the “print using macro” version, as opposed to “print via script” version.

      I simply added it to the “print via rule” code we are currently using to print 1 copy (which has worked flawlessly), but it doesn’t seem to want to print 2 copies (?).

      Seems this version needs something different when/if it’s run as a rule?

  4. Hi David,

    I’m having a hard time figuring out how outlook can print certain parts of emails automatically, that come from a certain email address. As an example to clarify my point, I receive emails everyday in the same format from the same email address. I would like to print about 5 or 6 specific parts from the email onto a label. through the rules i was able to automate this process to print the whole email, however i would only like specific information. I’ve read a couple of things you’ve done with respect to extracting a couple of lines and exporting them to excel. I was wondering is it’s possible to print them as appose to putting them to excel or maybe put them in excel as well.

    Regards,
    Elias

    • Hi, Elias.

      I’d need a sample of one of these emails and the details on what you want to pull from each of them. Can you provide a sample and those additional details?

    • Hi David,

      Yes I am able to provide an email for you, should I just forward it to your email ??

      Thank you very much for taking the time

    • Hi, Elias.

      It’s better if you send me an email with that message attached rather than forwarding it. Forwarding sometimes alters the formatting a bit. Sending it as an attachment will leave it as is.

  5. Hi Lee,

    First of all, Happy New Year!

    I am using Office 2016 now and your great creations don`t seem to work anymore. No error messages of any kind. Just nothing happens. Have you seen this behaviour yet?

    Best regards

    • Hi, Tech2.

      The code should work just fine in Office 2016. Since nothing is happening at all, are you certain that macros are enabled?

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