I have a VBA script which cycles through selected emails and prints all PDF attachments in said emails.
Currently, the script saves the PDF files to the hard drive and then opens and prints them.
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")
strTempFolder = "W:\my documents\test"
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
If InStr(strFilePath, ".pdf") <> 0 Or InStr(strFilePath, ".PDF") <> 0 Then
objAttachment.SaveAsFile (strFilePath)
Set objShell = CreateObject("Shell.Application")
Set objTempFolder = objShell.NameSpace(0)
Set objTempFolderItem = objTempFolder.ParseName(strFilePath)
objTempFolderItem.InvokeVerbEx ("print") 'try now
End If
Next objAttachment
End If
Next
End Sub
I am wondering if it is possible to execute this code without saving the files to the hard drive, i.e. just opening them from memory in VBA, print them, and have no trace of them on the hard drive?
Even "big" softwares, for example Outlook, are saving PDF-files to a local drive (temp-Folder) to open them in another program.
The problem here is that the PDF-reader needs a reference/path to the file you want to open.
I would suggest to delete the file from the drive after it is printed.