PDA

View Full Version : Batch print all attachments VBA



Bob2
05-24-2018, 08:10 AM
Hello,

So I wanted a VBA code that can print every pdf from multiple emails and I found this code that works perfectly. My question is, is it possible to add the format of paper I want to print on (Letter / 8,5 x 11) to the code below?

Thank you.

Sub BatchPrintAllAttachmentsinMultipleEmails()
Dim objFileSystem As Object
Dim strTempFolder As String
Dim objSelection As Outlook.Selection
Dim objItem As Object
Dim objMail As Outlook.MailItem
Dim objAttachments As Outlook.Attachments
Dim objAttachment As Outlook.Attachment
Dim objShell As Object
Dim objTempFolder As Object
Dim objTempFolderItem As Object
Dim strFilePath As String

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
strTempFolder = objFileSystem.GetSpecialFolder(2).Path & "\Temp for Attachments " & Format(Now, "YYYY-MM-DD_hh-mm-ss")
'Create a new temp folder
MkDir (strTempFolder)

Set objSelection = Outlook.Application.ActiveExplorer.Selection

For Each objItem In objSelection
If TypeOf objItem Is MailItem Then
Set objMail = objItem
Set objAttachments = objMail.Attachments

'Save all the attachments in the temp folder
For Each objAttachment In objAttachments
strFilePath = strTempFolder & "" & objAttachment.FileName
objAttachment.SaveAsFile (strFilePath)

'Print all the files in the temp folder
Set objShell = CreateObject("Shell.Application")
Set objTempFolder = objShell.NameSpace(0)
Set objTempFolderItem = objTempFolder.ParseName(strFilePath)
objTempFolderItem.InvokeVerbEx ("print")
Next objAttachment
End If
Next
End Sub

gmayor
05-24-2018, 10:29 PM
The short answer is no and you have made no allowance for the fact that e.g. graphics in messages are treated as separate attachments. This could result in a mess unless you tie down what attachments need to be printed; and if you do that it is probably better to load the attachments into their native applications, make whatever changes you need then print them from there. This should work for Office documents, others may not be so straightforward.