PDA

View Full Version : Outlook VBA macro batch printing attachment multiple e-mail



PatrickS15
10-12-2017, 06:28 AM
Hello,
About a week ago, I found a macro to batch print attachments, from the emails which I would select.
It works flawlessly and prints out all of the attachments within the selected emails.
However I have recognized a problem with the macro afterall, which is that the macro doesn't print the emails in any sort of order.
Is there by any chance, a way to make the macro print the emails from the top, and work it's way down through the folder?
At the time of writing, the macro will print in an unwanted order.
To clarify what I mean, I want it to print out with the most recent email being the 1st, and the 2nd most recent email being the 2nd printed.
Right now the order is somewhat messed up, and it would probably print in an order like "1, 2, 4, 3, 5, 7, 6, 8", whereas I would like it to print in a chronological order "1, 2, 3, 4, 5, 6, 7, 8".
Is this possible to adjust the macro, so it will print the attachments in order? :)
My macro is as listed below:



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

SamT
10-16-2017, 12:54 PM
bump

gmayor
10-24-2017, 09:21 PM
Oops - wrong thread. :(