【问题标题】:Batch printing attachments, with same name, from multiple emails批量打印来自多封电子邮件的同名附件
【发布时间】:2018-05-18 11:27:53
【问题描述】:

我在 Outlook 2013 中有电子邮件,每封电子邮件都只附有一个名为“Report.pdf”的文件。

我正在尝试批量打印我选择的电子邮件中的所有附件。

我发现下面的代码在附件都具有不同名称的情况下有效。是否可以修改它以打印近 150 个同名的附件?

报告的名称无关紧要,因此请随时在代码中添加您需要的内容。

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
Dim DateFormat

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

【问题讨论】:

    标签: vba printing outlook email-attachments


    【解决方案1】:

    如果所有附件都具有相同的名称,您不会说明为什么代码不起作用。我认为这是因为SaveAsFile 想在打印完成之前用下一个“Report.pdf”覆盖最后一个“Report.pdf”。

    我的第一个想法是在SaveAsFile 之前添加Kill strFilePath。回想起来,我认为这行不通,因为当您尝试删除之前的“Report.pdf”时,Shell 仍会打印它。

    我认为最简单的方法是:

    添加

    Dim Count as Long
    

    到你的 Dims。

    strFilePath = strTempFolder & "\" & objAttachment.FileName 替换为:

    Count = Count + 1
    strFilePath = strTempFolder & "\" & Count & objAttachment.FileName
    

    这将创建并打印名为“1Report.pdf”、“2Report.pdf”、“3Report.pdf”等的文件。我使用了前缀而不是传统的后缀,因为它省去了在文件名和扩展名之间放置Count 的麻烦。

    我假设您有一些方法可以从临时文件夹中删除所有附件。

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2016-06-15
      • 2017-02-19
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      相关资源
      最近更新 更多