>我有一个VBA脚本,它可以循环浏览选定的电子邮件并打印所述电子邮件中的所有PDF附件。
目前,该脚本将 PDF 文件保存到硬盘驱动器,然后打开并打印它们。
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 documentstest"
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
我想知道是否可以在不将文件保存到硬盘驱动器的情况下执行此代码,即只需从 VBA 中的内存中打开它们,打印它们,并且在硬盘驱动器上没有它们的痕迹?
即使是"大"软件,例如Outlook,也会将PDF文件保存到本地驱动器(临时文件夹(以在另一个程序中打开它们。
这里的问题是PDF阅读器需要您要打开的文件的引用/路径。
我建议在打印后从驱动器中删除该文件。