VBOffice

Print Attachments Automatically

See how to print automatically the attachments you receive. A second sample shows how to print the attachments of all selected emails.

Last modified: 2017/09/11 | Accessed: 152.475  | #3
◀ Previous sample Next sample ▶

Content

SAM SAM
Determine the "identity" of your emails. Set with SAM the sender and the folder folder for sent items with the help of rules.

Print Automatically When Emails Arrive

You can print attachments automatically as soon as you receive them by email. It only requires that there's an application installed on your computer that can handle the type of file, e.g. Word for printing *.doc files etc. - and that the printer is running, of course.

See the Select Case statement in the PrintAttachments procedure where the file types are listed. At present only Excel Workbooks (*.xls), Word (*.doc) and PDF documents will be printed. Add any file type you need.

To be able to print an attachment, the macro first must save it as a file. See the sDirectory variable where you determine where to store the attachment. Change the value to a directory that does exist on your computer.


tip  How to add macros to Outlook
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 WithEvents Items As Outlook.Items

Private Sub Application_Startup()
  Dim Ns As Outlook.NameSpace
  Dim Folder As Outlook.MAPIFolder

  Set Ns = Application.GetNamespace("MAPI")
  Set Folder = Ns.GetDefaultFolder(olFolderInbox)
  Set Items = Folder.Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
  If TypeOf Item Is Outlook.MailItem Then
    PrintAttachments Item
  End If
End Sub

Private Sub PrintAttachments(oMail As Outlook.MailItem)
  On Error Resume Next
  Dim colAtts As Outlook.Attachments
  Dim oAtt As Outlook.Attachment
  Dim sFile As String
  Dim sDirectory As String
  Dim sFileType As String

  sDirectory = "D:Attachments"

  Set colAtts = oMail.Attachments

  If colAtts.Count Then
    For Each oAtt In colAtts

      sFileType = LCase$(right$(oAtt.FileName, 4))

      Select Case sFileType
      Case ".xls", ".doc", ".pdf"
        sFile = ATTACHMENT_DIRECTORY & oAtt.FileName
        oAtt.SaveAsFile sFile
        ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
      End Select
    Next
  End If
End Sub
OLKeeper OLKeeper
OLKeeper reliably prevents users from closing their Outlook window and thus possibly missing reminders or e-mails.

Print Attachments Of Selected Emails

This sample doesn´t print automatically but must be called manually. Then it prints the attachments of all selected emails. Subject to the customization (which file types to print, and where to store the files) see the comments for the first sample above.

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

Public Sub PrintSelectedAttachments()
  Dim Exp As Outlook.Explorer
  Dim Sel As Outlook.Selection
  Dim obj as Object
  Set Exp = Application.ActiveExplorer
  Set Sel = Exp.Selection
  For Each obj in Sel
    If TypeOf obj is Outlook.MailItem Then
      PrintAttachments obj
    EndIf
  Next
End Sub

Private Sub PrintAttachments(oMail As Outlook.MailItem)
  On Error Resume Next
  Dim colAtts As Outlook.Attachments
  Dim oAtt As Outlook.Attachment
  Dim sFile As String
  Dim sDirectory As String
  Dim sFileType As String

  sDirectory = "D:\Attachments"

  Set colAtts = oMail.Attachments

  If colAtts.Count Then
    For Each oAtt In colAtts

      sFileType = LCase$(right$(oAtt.FileName, 4))

      Select Case sFileType
      Case ".xls", ".doc", ".pdf"
        sFile = ATTACHMENT_DIRECTORY & oAtt.FileName
        oAtt.SaveAsFile sFile
        ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0
      End Select
    Next
  End If
End Sub
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message