Automatically print incoming emails

Another post in the Save Money, use VBA series.

Automatically print incoming emails

printer

To automatically print out emails using an event handler, see below. Starting with the code found at Stock Event Code, call the MailItem.PrintOut Method. Unfortunately, this method cannot be customized at all, so the email comes out very plain, with all the fields arranged as Outlook sees fit.

Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Set Items = GetItems(GetNS(GetOutlookApp), olFolderInbox)
End Sub

Private Sub Items_ItemAdd(ByVal item As Object)

  On Error GoTo ErrorHandler

  Dim Msg As Outlook.MailItem
  Dim destfldr As Outlook.MAPIFolder

  If Not IsMail(item) Then GoTo ProgramExit

  Set Msg = item

  Msg.PrintOut

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.number & " - " & Err.Description
  Resume ProgramExit
End Sub

Function IsMail(itm As Object) As Boolean
  IsMail = (TypeName(itm) = "MailItem")
End Function

Function GetItems(olNS As Outlook.NameSpace, _
    folder As OlDefaultFolders) As Outlook.Items
  Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
  Set GetOutlookApp = Outlook.Application
End Function

Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
  Set GetNS = app.GetNamespace("MAPI")
End Function

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

comment bubble 2 Comments:

  1. Jon Peltier writes:

    This only saves money if you have good
    filters and avoid printing all that spam.

Comments on this article are closed. Why?

Site last updated: February 12, 2012