Skip to main content

如何在Outlook中郵件到達時自動列印附件?

Author: Siluvia Last Modified: 2025-08-06

本教程展示了一種方法,結合VBA腳本和Outlook規則,幫助您在特定郵件到達Outlook時自動列印其附件。


當特定郵件到達時自動列印附件

假設您希望自動列印來自某個特定寄件人的郵件附件。您可以按照以下步驟操作來實現這一目標。

步驟1:在Outlook中創建腳本

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

1. 啟動您的Outlook,同時按下 Alt + F11 鍵以打開 Microsoft Visual Basic for Applications 窗口。

2. 在 Microsoft Visual Basic for Applications 窗口中,雙擊 Project1 > Microsoft Outlook Objects > ThisOutlookSession 以打開 ThisOutlookSession (Code) 窗口,然後將以下代碼複製到此代碼窗口中。

steps of auto printing attachments when emails arrive in Outlook

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. 在 Case "pdf" 這一行中,請將 "pdf" 更改為您想要列印的文件擴展名。

3. 接下來點擊 Tools > References。在彈出的 References – Project1 對話框中,勾選 Microsoft Scripting Runtime 選項,然後點擊 OK 按鈕。

steps of auto printing attachments when emails arrive in Outlook

4. 保存代碼並按下 Alt + Q 鍵以關閉 Microsoft Visual Basic for Applications 窗口。

注意:請確保已在您的Outlook中啟用了「啟用所有宏」選項。您可以通過以下步驟檢查此選項。

steps of auto printing attachments when emails arrive in Outlook
步驟2:建立使用該腳本的規則

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

1. 轉到 Home 頁籤,點擊 Rules > Manage Rules & Alerts

steps of auto printing attachments when emails arrive in Outlook

2. 在 Rules and Alerts 對話框中,點擊 New Rule 按鈕以創建規則。

提示:如果您已向Outlook添加了多個郵件帳戶,請在「應用更改到此文件夾」下拉列表中指定一個帳戶,以便應用該規則。否則,它將應用於當前選定郵件帳戶的收件匣。

steps of auto printing attachments when emails arrive in Outlook

3. 在第一個 Rules Wizard 對話框中,在 Step 1 框中選擇 Apply rule on messages I receive,然後點擊 Next。

steps of auto printing attachments when emails arrive in Outlook

4. 在第二個 Rules Wizard 對話框中,您需要:

4.1) 根據需要在 Step 1 框中指定一個或多個條件;
在此情況下,我只想列印來自指定寄件人的進來郵件中的附件。因此,我在這裡勾選 from people or public group 框。
4.2) 點擊 Step 2 框中的帶下劃線的值以編輯條件;
4.3) 點擊 Next。見截圖:
steps of auto printing attachments when emails arrive in Outlook

5. 在第三個 Rules Wizard 對話框中,您需要進行如下配置。

5.1) 在 Step 1: Select action(s) section 中,勾選 run a script 框;
5.2) 在 Step 2 部分,點擊帶下劃線的文字“a script”;
5.3) 在彈出的 Select Script 對話框中,點擊您上面添加的VBA代碼名稱,然後點擊 OK;
5.4) 點擊 Next 按鈕。見截圖:
steps of auto printing attachments when emails arrive in Outlook

提示:如果您的 Rules Wizard 中缺少「run a script」選項,可以通過本文提到的方法顯示它:恢復Outlook規則中缺失的 Run A Script 選項

6. 然後另一個 Rules Wizard 彈出詢問例外情況。如有必要,您可以選擇例外情況,否則不選擇任何內容直接點擊 Next 按鈕。

steps of auto printing attachments when emails arrive in Outlook

7. 在最後一個 Rules Wizard 中,您需要為規則指定一個名稱,然後點擊 Finish 按鈕。

steps of auto printing attachments when emails arrive in Outlook

8. 然後返回到 Rules and Alerts 對話框,您可以看到創建的規則已列出其中,點擊 OK 按鈕完成整個設置。

steps of auto printing attachments when emails arrive in Outlook

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


相關文章

僅列印Outlook中一封郵件或所選郵件的附件
在Outlook中,您可以列印郵件,但您是否列印過僅來自一封郵件或所選郵件的附件呢?本文介紹了解決這個問題的技巧。

僅列印Outlook中郵件的消息頭
在Outlook中列印郵件時,會同時列印消息頭和消息正文。然而,在某些特殊情況下,您可能只需要列印包含主題、寄件人、收件人等的消息頭。本文將介紹兩種解決方案。

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

在Outlook中列印帶有照片的聯繫人
通常,在Outlook中列印聯繫人時,不會列印聯繫人的照片。但有時,列印帶有照片的聯繫人會更加令人印象深刻。本文將介紹一些解決方法來實現這一目標。

在Outlook中列印郵件的一部分
如果您收到一封電子郵件,發現需要列印的是郵件內容的一部分而不是整封郵件,您會怎麼做?實際上,Outlook可以在互聯網瀏覽器(如Firefox和Internet Explorer)的幫助下幫助您完成此操作。這裡我將以互聯網瀏覽器為例。請看以下教程。

更多關於「在Outlook中列印」的文章...


最佳 Office 生產力工具

最新消息:Kutools for Outlook 推出免費版本!

體驗全新 Kutools for Outlook,擁有100+ 強大功能!立即下載!

🤖 Kutools AI 運用先進 AI 技術,輕鬆處理郵件,包括答覆、摘要、優化、擴寫、翻譯與撰寫郵件。

📧 郵件自動化自動回覆(支援 POP 和 IMAP) / 計劃發送郵件 / 發送郵件時根據規則自動抄送密送 / 自動轉發(高級規則) / 自動新增問候語 / 自動將多收件人郵件分割為個別郵件 ...

📨 郵件管理撤回郵件 /依主題等條件阻擋詐騙郵件 / 刪除重複郵件 / 高級搜索 / 整合文件夾 ...

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

🌟 介面魔法😊更多精美酷炫表情符號 /重要郵件來臨提醒 / 最小化 Outlook 而非關閉 ...

👍 一鍵神技帶附件全部答復 /反釣魚郵件 / 🕘顯示發件人時區 ...

👩🏼‍🤝‍👩🏻 聯絡人與日曆批次從選中郵件新增聯絡人 / 將聯絡人組分割為多個組 / 移除生日提醒 ...

以您偏好的語言使用 Kutools —— 支援英語、西班牙語、德語、法語、中文及40 多種其他語言!

只需一鍵即可立即啟用 Kutools for Outlook。立即下載,提升您的效率!

kutools for outlook features1 kutools for outlook features2