Outlook Macro: Automatically insert page breaks between replies and print the e-mail thread in Outlook 2007

A while back, a customer asked if it would be possible to automatically insert page breaks between replies (have each e-mail in a thread on a separate page) and print the e-mail thread in Outlook 2007.

Here is the way to do it:

Sub ProcessSelection()

Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim olAExp As Outlook.Explorer
Dim olSel As Outlook.Selection

Set olApp = Application
Set olNS = olApp.GetNamespace("MAPI")
Set olAExp = olApp.ActiveExplorer
Set olSel = olAExp.Selection
'get the current Outlook selection
For Each Item In olSel
'get mail messages in selection
If (Item.Class = olMail) Then
ProcessMessage (Item)
End If
Next Item

Set olSel = Nothing
Set olAExp = Nothing
Set olNS = Nothing
Set olApp = Nothing

End Sub

Sub ProcessMessage(ByVal olItem As Outlook.MailItem)

Dim path As String
path = olItem.Subject
'replace any unaccepted characters in the subject by spaces
path = Replace(path, ":", " ")
path = Replace(path, "\", " ")
path = Replace(path, "/", " ")
path = Replace(path, "*", " ")
path = Replace(path, strQuote, " ")
path = Replace(path, "<", " ")
path = Replace(path, ">", " ")
path = Replace(path, "|", " ")
path = "C:\" & path & ".doc"
'save the e-mail message as word document
olItem.SaveAs path, OlSaveAsType.olDoc
BreakAndPrint (path)
'delete the file from the disk
Kill path
End Sub

Sub BreakAndPrint(path As String)

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
'create a new word instance
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdApp.Documents.Open (path)
Set wdDoc = wdApp.ActiveDocument
wdApp.WindowState = wdWindowStateMaximize
'this will insert a page break before each instance of From:
With wdApp.Selection.Find
.Text = "From:"

End With
Do While wdApp.Selection.Find.Execute
wdApp.Selection.Collapse wdCollapseStart
wdApp.Selection.InsertBreak WdBreakType.wdPageBreak
wdApp.Selection.MoveDown wdLine, 1
'this will show the print dialog
'this will close the word document and application

Set wdDoc = Nothing
Set wdApp = Nothing

End Sub

The above macro gets the current Outlook 2007 selection so all you need to do is select a message in the message list in Outlook and run the macro. Next, the macro saves the Outlook message as a Word document, inserts the breakpoints, shows the print dialog and closes the Word document.

To add the macro code:

  1. On the Tools menu, point to Macro, and then click Macros.
  2. In the Macro name box, type a name for your macro, and then click Create. The Microsoft Visual Basic Editor opens.
  3. Type or paste your code into the code window of the module. If you need more information about coding, click Microsoft Visual Basic Help on the Help menu.
  4. When you are finished writing your macro, click Close and Return to Microsoft Office Outlook on the File menu.

To add a macro button to an Outlook 2007 toolbar:

  1. First put the Toolbar in Edit Mode;

    View-> Toolbars-> Customize
  2. Verify that the Commands tab is selected.
  3. In the Categories list on the right, select Macros.

    The Commands list on the right will now display all you macros

  4. Drag and drop the macro that you wish to create a button for to the location on the toolbar where you want it.
  5. To modify the name and icon, right click the icon on the Toolbar;
    • You can edit the name to something more meaningful.

      If you want to use an ALT+key shortcut for the button, place an & for the letter that you want to use.

      For instance, let’s assume you call it “My Button” and want to use the ALT+Y combination.

      You’d then write “M&y Button

    • You can change the button image via the Change Button Image selector or free hand via the Edit Button Image… dialog.

      In the free hand editor, you can actually paste another image or icon you have copied from via CTRL+V.

    • You can also disable the text or button or image if you don’t like to see it.
  6. Close the Customize dialog to leave Edit Mode of your Toolbars and use your button.
Comments (1)

  1. Horatio says:

    Could this code be adapted for Outlook 2010? I have tried it in Outlook 2010 but it doesn't work.

Skip to main content