2017-08-23 69 views
1

我有一個VBA腳本,它循環選定的電子郵件並打印所有電子郵件中的PDF附件。打印Outlook附件而不保存在硬盤上

目前,該腳本將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 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 

我想知道是否有可能不將文件保存到硬盤,即剛剛從內存中VBA打開它們執行此代碼,打印出來,並讓他們的硬盤驅動器上的無痕?

+1

即使是「大」軟件,例如Outlook,也會將PDF文件保存到本地驅動器(臨時文件夾)以在另一個程序中打開它們。這裏的問題是PDF閱讀器需要一個你想打開的文件的參考/路徑。爲什麼不在打印後從驅動器中刪除文件? – MatSnow

+0

我在使用VBA刪除文件夾及其文件時遇到了麻煩,這就是爲什麼我一直在尋找解決方案而沒有明確地將文件保存到硬盤。但是,我在VBA中刪除了文件和文件夾以便工作,所以您的建議對我來說最終還是有效的! :)是否可以設置此問題解決/回答? – user2011985

回答

0

即使「大」軟件(例如Outlook)也將PDF文件保存到本地驅動器(臨時文件夾)以在另一個程序中打開它們。
這裏的問題是,PDF閱讀器需要一個你想打開的文件的引用/路徑。

我建議打印後從驅動器中刪除文件。

相關問題