sometimes you might in need where you would like to print attachments for bulk emails. This type of requirement can be automated using power of VBA (Visual Basic for Applications). In this PoC we will learn code to print attachments in selected email item.
Code example
#If win7 Then Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As LongPtr) As LongPtr #Else 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 #End If Private Sub PrintAttachmentsSelectedMsg() 'Outlook application object declaration Dim oApp As Outlook.Application 'bind current outlook instance Set oApp = GetObject("", "Outlook.Application") Dim oMail As Outlook.MailItem Dim oItem As Object 'Iterate each mail in selection For Each oItem In oApp.ActiveExplorer.Selection If oItem.Class = olMail Then Set oMail = oItem Dim colAttachments As Outlook.Attachments Dim oAttchment As Outlook.Attachment Dim oFile As String Dim sDirectory As String Dim sFileType As String sDirectory = "C:\Users\Download\" Set colAttachments = oMail.Attachments 'Iterate each attachment For Each oAttchment In colAtts 'Save attachment oFile = sDirectory & oAttchment.Filename oAttchment.SaveAsFile oFile 'Print ShellExecute 0, "print", oFile, vbNullString, vbNullString, 0 Next oAttchment End If Next oItem 'CleanUp If Not oApp Is Nothing Then Set oApp = Nothing End If If Not oMail Is Nothing Then Set oMail = Nothing End If If Not oItem Is Nothing Then Set oItem = Nothing End If End Sub
Explanation
The above code will pick each selected item (mail) and look for attachments, if found, it will save a copy of the attachment in the given path and print the same.
Next >> Address List in Outlook Add-in using VBA code