Print PDF from Microsoft Outlook

This post contains a VBA macro that will set the output file name to a cleaned version of the subject. The selected mail item is then printed.

Note that Outlook remembers the last printer used. Therefore you have to use the PDF printer before you run this macro. Otherwise, Outlook will just print to another printer.

Option Explicit

Public Sub PrintPdf()
  Dim mi As MailItem
  Dim o
  Dim p As Integer
  Dim subj As String
  Dim illegalchars As String
  Dim i As Integer
  Dim output As String
  Dim settings As Object
  For Each o In Application.ActiveExplorer.Selection
    If TypeName(o) = "MailItem" Then
       Set mi = o
       subj = mi.Subject
       Rem -- Clean reply and forward prefixes
         p = InStr(1, Left(subj, 4), ":")
         subj = Trim(Mid(subj, p + 1))
       Loop While p > 0
     Rem -- Remove illegeal file  name characters
     illegalchars = "<>:""/\|?*" 
     For i = 1 To Len(illegalchars)
       subj = Replace(subj, Mid(illegalchars, i, 1), "_")
      Rem -- Remove double underscores
        p = InStr(1, subj, "__")
        subj = Replace(subj, "__", "_")
      Loop While p > 0
      Rem -- Create the COM settings object to control the printer.
      output = "C:\Temp\" & subj & ".pdf"
      Set settings = CreateObject("bullzip.PdfSettings")
      settings.SetValue "Output", output
      settings.SetValue "RememberLastFolderName", "no"
      Rem -- Write settings to the runonce.ini.
      settings.WriteSettings True
      Rem -- Print the object
      Debug.Print "Printing: " & subj
    End If
    Exit For
End Sub

When you have created the macro, you can customize your ribbon and give it an icon.