跳到主要內容

電子郵件到達 Outlook 時如何自動打印附件?

本教程演示了一種結合 VBA 腳本和 Outlook 規則的方法,以幫助您在某些電子郵件到達 Outlook 時自動打印它們的附件。


某些電子郵件到達時自動打印附件

假設您想自動打印來自某個發件人的傳入電子郵件的附件。 您可以執行以下操作來完成它。

第 1 步:在 Outlook 中創建腳本

首先,您需要在 Outlook 中創建一個 VBA 腳本。

1.啟動Outlook,按 其他 + F11 同時打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口,雙擊 Project1 > Microsoft Outlook對象 > 本次展望會議 打開 ThisOutlookSession(代碼) 窗口,然後將以下代碼複製到此代碼窗口中。

VBA 代碼 1:電子郵件到達時自動打印附件(所有類型的附件)

Sub AttachementAutoPrint(Item As Outlook.MailItem)
'Updated by Extendoffice 20230223
  Dim xFS As FileSystemObject
  Dim xTempFolder As String
  Dim xAtt As Attachment
  Dim xShell As Object
  Dim xFolder As Object, xFolderItem As Object
  Dim xFileName As String
  On Error GoTo xError
  If Item.Attachments.Count = 0 Then Exit Sub
  Set xFS = New FileSystemObject
  xTempFolder = xFS.GetSpecialFolder(TemporaryFolder)
  xTempFolder = xTempFolder & "\ATMP" & Format(Item.ReceivedTime, "yyyymmddhhmmss")
  If Not xFS.FolderExists(xTempFolder) Then
    MkDir (xTempFolder)
  End If
  Set xShell = CreateObject("Shell.Application")
  Set xFolder = xShell.NameSpace(0)
  For Each xAtt In Item.Attachments
    If IsEmbeddedAttachment(xAtt) = False Then
      xFileName = xTempFolder & "\" & xAtt.FileName
      xAtt.SaveAsFile (xFileName)
      Set xFolderItem = xFolder.ParseName(xFileName)
      xFolderItem.InvokeVerbEx ("print")
    End If
  Next xAtt
  Set xFS = Nothing
  Set xFolder = Nothing
  Set xFolderItem = Nothing
  Set xShell = Nothing
xError:
  If Err <> 0 Then
    MsgBox Err.Number & " - " & Err.Description, , "Kutools for Outlook"
    Err.Clear
  End If
Exit Sub
End Sub

Function IsEmbeddedAttachment(Attach As Attachment)
Dim xItem As MailItem
Dim xCid As String
Dim xID As String
Dim xHtml As String
On Error Resume Next
IsEmbeddedAttachment = False
Set xItem = Attach.Parent
If xItem.BodyFormat <> olFormatHTML Then Exit Function
xCid = ""
xCid = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
If xCid <> "" Then
    xHtml = xItem.HTMLBody
    xID = "cid:" & xCid
    If InStr(xHtml, xID) > 0 Then
        IsEmbeddedAttachment = True
    End If
End If
End Function

注意: 此代碼支持打印電子郵件中收到的所有類型的附件。 如果您只想打印指定類型的附件,例如 pdf 文件,請應用以下 VBA 代碼。

VBA代碼2:郵件到達時自動打印指定類型的附件

Sub AttachementAutoPrint(Item As Outlook.MailItem)
'Updated by Extendoffice 20230223
  Dim xFS As FileSystemObject
  Dim xTempFolder As String
  Dim xAtt As Attachment
  Dim xShell As Object
  Dim xFolder As Object, xFolderItem As Object
  Dim xFileType As String, xFileName As String
  On Error GoTo xError
  If Item.Attachments.Count = 0 Then Exit Sub
  Set xFS = New FileSystemObject
  xTempFolder = xFS.GetSpecialFolder(TemporaryFolder)
  xTempFolder = xTempFolder & "\ATMP" & Format(Item.ReceivedTime, "yyyymmddhhmmss")
  If Not xFS.FolderExists(xTempFolder) Then
    MkDir (xTempFolder)
  End If
  Set xShell = CreateObject("Shell.Application")
  Set xFolder = xShell.NameSpace(0)
  For Each xAtt In Item.Attachments
    If IsEmbeddedAttachment(xAtt) = False Then
      xFileName = xAtt.FileName
      xFileType = LCase$(Right$(xFileName, VBA.Len(xFileName) - VBA.InStrRev(xFileName, ".")))
      xFileName = xTempFolder & "\" & xFileName
      Select Case xFileType
        Case "pdf"   'change "pdf" to the file extension you want to print
          xAtt.SaveAsFile (xFileName)
          Set xFolderItem = xFolder.ParseName(xFileName)
          xFolderItem.InvokeVerbEx ("print")
      End Select
    End If
  Next xAtt
  Set xFS = Nothing
  Set xFolder = Nothing
  Set xFolderItem = Nothing
  Set xShell = Nothing
xError:
  If Err <> 0 Then
    MsgBox Err.Number & " - " & Err.Description, , "Kutools for Outlook"
    Err.Clear
  End If
  Exit Sub
End Sub

Function IsEmbeddedAttachment(Attach As Attachment)
Dim xItem As MailItem
Dim xCid As String
Dim xID As String
Dim xHtml As String
On Error Resume Next
IsEmbeddedAttachment = False
Set xItem = Attach.Parent
If xItem.BodyFormat <> olFormatHTML Then Exit Function
xCid = ""
xCid = Attach.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F")
If xCid <> "" Then
    xHtml = xItem.HTMLBody
    xID = "cid:" & xCid
    If InStr(xHtml, xID) > 0 Then
        IsEmbeddedAttachment = True
    End If
End If
End Function

筆記:

1.在應用此VBA代碼僅打印傳入電子郵件中的pdf文件之前,首先需要下載並安裝 使用Adobe Acrobat Reader 並將其設置為計算機中的默認 pdf 閱讀器。
2. 排隊 案例“pdf”, 請更換 “PDF” 到要打印的文件擴展名。

3. 繼續點擊 工具 > 引用。 在彈出 參考 - 項目1 對話框,檢查 Microsoft腳本運行時 框,然後單擊 OK 按鈕。

4.保存代碼,然後按 其他 + Q 關閉鍵 Microsoft Visual Basic for Applications 窗口。

注意: 請確保 啟用所有的宏 Outlook 中啟用了該選項。 您可以按照下面顯示的步驟檢查此選項。

Step2:構建規則以使用腳本

在 Outlook 中添加 VBA 腳本後,您需要創建規則以根據某些條件使用該腳本。

1. 轉到主頁選項卡,單擊 規則 > 管理規則和警報.

2。 在裡面 規則和警報 對話框中,單擊 新規則 按鈕以創建規則。

提示: 如果您在 Outlook 中添加了多個電子郵件帳戶,請在 將更改應用到此文件夾 要應用規則的下拉列表。 否則,它將應用於當前選擇的電子郵件帳戶的收件箱。

3.在第一 規則嚮導 對話框中選擇 對我收到的消息應用規則 ,在 步驟 1 框,然後單擊 下一步。

4.在第二 規則嚮導 對話框,您需要:

4.1) 指定一個或多個條件 步驟 1 根據您的需要進行包裝;
在這種情況下,我只想打印來自指定發件人的傳入電子郵件中的附件。 在這裡,我檢查 來自人或公共團體 框。
4.2) 點擊帶下劃線的值 步驟 2 框以編輯條件;
4.3)點擊 下一步。 見截圖:

5.在第三 規則嚮導 對話框,您需要配置如下。

5.1)在 第 1 步:選擇操作部分, 檢查 運行腳本 框;
5.2)在 步驟 2 部分,單擊帶下劃線的文本“腳本”;
5.3)在開頭 選擇腳本 對話框中,單擊上面添加的 VBA 代碼的名稱,然後單擊 好;
5.4)點擊 下一頁 按鈕。 看截圖:

提示: 如果“運行腳本” 選項在您的 規則嚮導,您可以按照本文中提到的方法進行顯示: 恢復 Outlook 規則中缺少的運行腳本選項.

6.然後另一個 規則嚮導 彈出詢問例外情況。 如有必要,您可以選擇例外,否則,單擊 下一頁 沒有任何選擇的按鈕。

7.最後 規則嚮導,您需要為規則指定一個名稱,然後單擊 按鈕。

8.然後返回到 規則和警報 對話框,你可以看到你創建的規則列在裡面,點擊 OK 按鈕以完成整個設置。

從現在開始,當收到來自指定人員的電子郵件時,將自動打印附件。


相關文章

僅從一封電子郵件或 Outlook 中選定的電子郵件打印附件
在 Outlook 中,您可以打印電子郵件,但您是否只打印了一封電子郵件或 Outlook 中選定電子郵件的附件? 本文介紹解決此工作的技巧。

在 Outlook 中僅打印電子郵件的郵件標題
在 Outlook 中打印電子郵件時,它將同時打印電子郵件中的郵件標題和郵件正文。 但是,在某些特殊情況下,您可能只需要打印出帶有主題、發件人、收件人等的郵件標題。本文將介紹兩種解決方案。

在 Outlook 的指定/自定義日期範圍內打印日曆
通常,在 Outlook 的月視圖中打印日曆時,它會自動選擇包含當前選定日期的月份。 但是,您可能需要在自定義日期範圍內打印日曆,例如 3 個月、半年等。本文將為您介紹解決方案。

在 Outlook 中打印帶圖片的聯繫人
通常,在Outlook中打印聯繫人時,不會將其圖片打印出來。 但是有時,打印帶有圖片的聯繫人會給人留下深刻的印象。 本文將介紹一些解決方法來完成它。

在 Outlook 中打印電子郵件的選擇
如果您收到一封電子郵件,並且發現需要打印一些電子郵件內容,而不是打印整個郵件,您將怎麼辦? 實際上,Outlook可以藉助Internet瀏覽器(例如​​Firefox和Internet Explorer)幫助您實現此操作。 在這裡,我將以Internet瀏覽器為例。 請查看以下教程。

更多關於“在 Outlook 中打印”的文章...


最佳辦公生產力工具

Kutools for Outlook - 超過 100 種強大的功能可增強您的 Outlook

🤖 人工智慧郵件助手: 具備人工智慧魔力的即時專業電子郵件——一鍵天才回覆、完美語調、多語言掌握。輕鬆改變電子郵件! ……

📧 電子郵件自動化: 外出(適用於 POP 和 IMAP)  /  安排發送電子郵件  /  發送電子郵件時按規則自動抄送/密件副本  /  自動轉送(進階規則)   /  自動添加問候語   /  自動將多收件者電子郵件拆分為單獨的訊息 ...

📨 電子郵件管理: 輕鬆回憶電子郵件  /  按主題和其他人阻止詐騙電子郵件  /  刪除重複的電子郵件  /  進階搜索  /  合併資料夾 ...

📁 附件專業版批量保存  /  批量分離  /  批量壓縮  /  自動保存   /  自動分離  /  自動壓縮 ...

🌟 介面魔法: 😊更多又漂亮又酷的表情符號   /  使用選項卡式視圖提高 Outlook 工作效率  /  最小化 Outlook 而不是關閉 ...

👍 一鍵奇蹟: 使用傳入附件回覆全部  /   反網路釣魚電子郵件  /  🕘顯示寄件者的時區 ...

👩🏼‍🤝‍👩🏻 通訊錄和行事曆: 從選定的電子郵件中大量新增聯絡人  /  將聯絡人群組拆分為各組  /  刪除生日提醒 ...

超過 100特點 等待您的探索! 按此處了解更多。

了解更多       免費下載      購買
 

 

Comments (22)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Kan deze script ook gemaakt worden voor het verzenden van emails, dat je dan automatisch de bijlage kan laten afdrukken ?
This comment was minimized by the moderator on the site
Hello, thank you very much for the scripts, very useful indeed!
What if we wanted to print all attachments in an email in Outlook 365 web instead? Are there ways to try this?
This comment was minimized by the moderator on the site
Hi is there a way to print the email body including sender details and subject line in the script please. I pretty much need email and attachment printed out please.
This comment was minimized by the moderator on the site
Hi Mani,

If you want to print an email body including sender details and subject line, I suggest you try the Advanced Print feature of Kutools for Outlook. It can help you solve the problem easily.
https://www.extendoffice.com/images/stories/comments/comment-picture-zxm/advanced-print.png?1696644946
This comment was minimized by the moderator on the site
First of all, thanks a lot for these useful VBA scripts!
My situation is as follows: I usually receive a number of emails with 2 pdf files each. One pdf file, let's call it example1.pdf, needs to be printed only once, but the second pdf file, let's call it example2.pdf, needs to be printed 3 times. The example2.pdf does not necessarily always have the same name, but there are only around 2-4 name variants, so if it were possible to tell the script to look out for a few keywords I think it would work. Is this possible?
Oh, and would it be possible to also tell the script to print the example1.pdf file as duplex?
Thanks for your help.
This comment was minimized by the moderator on the site
Hi There, thanks for publishing this

I have followed the instructions above and are running the script to print all attachments delivered.

we typically receive these in batches of 5-8 and when testing i am getting 4 out of 5 prints with one failing with error 75. All PDF's have different names and are on separate emails/ the attachements can be opened manually fine so i assume theres an issue when it tries to copy this to the temp directory. Do you have any idea how we can resolve this?
This comment was minimized by the moderator on the site
Same problem here. After a couple of emails received during a short time, it can print 3-4 pdf, but after the error 75 appear, nothing print in the same batch of mails.
This comment was minimized by the moderator on the site
Hi Gabriel,
After testing, we have reproduced the problem you encountered. the VBA scripts have been updated in the post. Please give them a try. Thanks for your feedback.
This comment was minimized by the moderator on the site
Love this script! How about if I get an email with multiple pdfs and want one copy of each. Currently when I get 3 pdf's attached, it prints the final one, 3 times, and the other 2 do not get printed. Thanks for any help!
This comment was minimized by the moderator on the site
Hi val,

Sorry for the inconvenience.
Which VBA code are you applying? VBA code 1 or VBA code 2? Do the three PDF attachments have the same name? And which Outlook version are you using?
I have tested the codes and they worked well in my case. I need to know more specific about your issue.
This comment was minimized by the moderator on the site
Is it possible to specify the printer that should be used (not the system default printer)?
I need to print 2 types of documents on 2 different printers....
Thanks for your help!
This comment was minimized by the moderator on the site
Hi Simon,
Thank you for your comment. After trying with various methods, I can't seem to get this to work. Sorry for the inconvenience.
This comment was minimized by the moderator on the site
Hello, kindly I need help, I can't integrate the script for printing pdf only.
I activated everything, the first script for generic printing works perfectly (or almost: printing pdf attachments once printed acrobat reader remains open in the background), but the specific script for pdf does not work. Thanks for posting the scripts and for any help.
Good day
This comment was minimized by the moderator on the site
Hi Marco041,
There was something wrong with the code and it has been updated. Please give it a try.
Note: we have no way to prevent Acrobat Reader from being opened because we need to use it to open the pdf document for the next print job.
Thank you for your feedback.
Sub AttachementAutoPrint(Item As Outlook.MailItem)
'Updated by Extendoffice 20220920
  Dim xFS As FileSystemObject
  Dim xTempFolder As String
  Dim xAtt As Attachment
  Dim xShell As Object
  Dim xFolder As Object, xFolderItem As Object
  Dim xFileType As String, xFileName As String
  On Error GoTo xError
  Set xFS = New FileSystemObject
  xTempFolder = xFS.GetSpecialFolder(TemporaryFolder)
  xTempFolder = xTempFolder & "\ATMP" & Format(Now, "yyyymmddhhmmss")
  MkDir (xTempFolder)
  'Set Item = Application.ActiveExplorer.Selection.Item(1)
  Set xShell = CreateObject("Shell.Application")
  Set xFolder = xShell.NameSpace(0)
  For Each xAtt In Item.Attachments
    xFileName = xAtt.FileName
    xFileType = LCase$(Right$(xFileName, VBA.Len(xFileName) - VBA.InStrRev(xFileName, ".")))
    xFileName = xTempFolder & "\" & xFileName
    xAtt.SaveAsFile (xFileName)
    Select Case xFileType
      Case "pdf"   'change "pdf" to the file extension you want to print
        Set xFolderItem = xFolder.ParseName(xFileName)
        xFolderItem.InvokeVerbEx ("print")
    End Select
  Next xAtt
'xFS.DeleteFolder (xTempFolder)
Set xFS = Nothing
Set xFolder = Nothing
Set xFolderItem = Nothing
Set xShell = Nothing
xError:
  If Err <> 0 Then
    MsgBox Err.Number & " - " & Err.Description, , "Kutools for Outlook"
    Err.Clear
  End If
Exit Sub
End Sub
This comment was minimized by the moderator on the site
Bonjour question pour vous j'ai fait les étapes pour cela mais dans les règle de message de mon outlook je ne voit pas que je peux utilisé un script
This comment was minimized by the moderator on the site
Hi STEPHANE CADORETTE,
If the “run a script” option is missing in your Rules Wizard, you can display it by following the method mentioned in this article: restore missing Run A Script pption in Outlook rule.
This comment was minimized by the moderator on the site
Bonjour moi j'ai un petit soucie lorsque j'ai fait et refait les étapes mais lorsque je créer ma règle je n'Ai pas l'option run a script.
This comment was minimized by the moderator on the site
Hi Stéphane,
If the “run a script” option is missing in your Rules Wizard, you can display it by following the method mentioned in this article: restore missing Run A Script pption in Outlook rule.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations