Outlook - Print all attachments at once (or: Handle attachments with VBA)

* Update for Outlook 2010/2013 users: there is a new plugin that can do all of the below code without any code writing, Download it here : 

http://udi.intermaxweb.biz/PrintAttachments/

 
If you want to have the ability to print all the attachments (from a mail item) at once, than this post is for you (also if you just want to know how to handle attachments using VBA in Outlook):

This post content is only to show you how to handle attachments in a VBA code , you can print your attachments in many other ways as well, but here you can learn how to do outlook VBA attachments handling
.

(this post's screen shots were taken from Microsoft Outlook 2003)


The instructions:

Outlook -> Click Alt-F11 to open the VBA Editor

Or use “Tools” Menu - “Macro” - “Visual Basic Editor”

outlook VBA attachments handling


Right Click The "Project1" Line And Select Insert Module


outlook VBA attachments handling

In the new added module (if it’s your first module then it is named by default “Module1”) paste the following code:


AVG products are running on over 80 million computers worldwide, find out why.


-->
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
Sub printall()
Dim CurrentMailItem As MailItem
Dim attCounter As Integer
Dim loopCounter As Integer
Dim attFileName As String
Dim errMsg As String
On Error GoTo err
CurrentMailItem = Application.ActiveInspector.CurrentItem
attCounter = CurrentMailItem.Attachments.Count
For loopCounter = 1 To attCounter
attFileName = CurrentMailItem.Attachments.Item(loopCounter).FileName
CurrentMailItem.Attachments.Item(loopCounter).SaveAsFile( _
Environ$("TEMP") & "\" & attFileName)
ShellExecute(0, "print", Environ$("TEMP") & "\" & attFileName, "", "", 1)
Kill(Environ$("TEMP") & "\" & attFileName)
DoEvents()
Next
Exit Sub
err:
IIf(attFileName <> "", errMsg = "Last file handeld was : " & _
attFileName & ".", errMsg = "No file was handeld.")
MsgBox("Error printing all attachments." & vbCrLf & errMsg & _
vbCrLf & vbCrLf & err.Description, vbCritical, "Error number " & _
err.Number)
End Sub
-->



You can download the module from here (for "Copy-Paste" issues)

Save the module and Close the VBA Editor.
Open any received mail message.
Right Click one of the toolbars and select “Customize…”

Outlook toolbar customize macro

Drag the “Project1.printall” Macro to one of your toolbars

Outlook toolbar customize macro

Change the new button name and icon

Outlook toolbar customize macro


Close the message.
here ends the "Outlook toolbar customize macro" thing




How to use it:
Open the message with the attachments
Click your new button and all your attachments (at the current opened item) will wait for you at your printer tray.

Pay attention:
You will print only to the last printer used by outlook.



1 comment:

  1. Wonderful, this could be a start.

    What I would like to add is a line in the header giving the attachment's name plus pages/page number or "x of z", or whatever.

    This would make it for the reader transparent, whoever prints it usually is not the one who has to deal with it. That's why.

    ReplyDelete