電子郵件到達 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
筆記:
3. 繼續點擊 工具 > 引用。 在彈出 參考 - 項目1 對話框,檢查 Microsoft腳本運行時 框,然後單擊 OK 按鈕。
4.保存代碼,然後按 其他 + Q 關閉鍵 Microsoft Visual Basic for Applications 窗口。
注意: 請確保 啟用所有的宏 Outlook 中啟用了該選項。 您可以按照下面顯示的步驟檢查此選項。
Step2:構建規則以使用腳本
在 Outlook 中添加 VBA 腳本後,您需要創建規則以根據某些條件使用該腳本。
1. 轉到主頁選項卡,單擊 規則 > 管理規則和警報.
2。 在裡面 規則和警報 對話框中,單擊 新規則 按鈕以創建規則。
提示: 如果您在 Outlook 中添加了多個電子郵件帳戶,請在 將更改應用到此文件夾 要應用規則的下拉列表。 否則,它將應用於當前選擇的電子郵件帳戶的收件箱。
3.在第一 規則嚮導 對話框中選擇 對我收到的消息應用規則 ,在 步驟 1 框,然後單擊 下一步。
4.在第二 規則嚮導 對話框,您需要:
5.在第三 規則嚮導 對話框,您需要配置如下。
提示: 如果“運行腳本” 選項在您的 規則嚮導,您可以按照本文中提到的方法進行顯示: 恢復 Outlook 規則中缺少的運行腳本選項.
6.然後另一個 規則嚮導 彈出詢問例外情況。 如有必要,您可以選擇例外,否則,單擊 下一頁 沒有任何選擇的按鈕。
7.最後 規則嚮導,您需要為規則指定一個名稱,然後單擊 完 按鈕。
8.然後返回到 規則和警報 對話框,你可以看到你創建的規則列在裡面,點擊 OK 按鈕以完成整個設置。
從現在開始,當收到來自指定人員的電子郵件時,將自動打印附件。
相關文章
僅從一封電子郵件或 Outlook 中選定的電子郵件打印附件
在 Outlook 中,您可以打印電子郵件,但您是否只打印了一封電子郵件或 Outlook 中選定電子郵件的附件? 本文介紹解決此工作的技巧。
在 Outlook 中僅打印電子郵件的郵件標題
在 Outlook 中打印電子郵件時,它將同時打印電子郵件中的郵件標題和郵件正文。 但是,在某些特殊情況下,您可能只需要打印出帶有主題、發件人、收件人等的郵件標題。本文將介紹兩種解決方案。
在 Outlook 的指定/自定義日期範圍內打印日曆
通常,在 Outlook 的月視圖中打印日曆時,它會自動選擇包含當前選定日期的月份。 但是,您可能需要在自定義日期範圍內打印日曆,例如 3 個月、半年等。本文將為您介紹解決方案。
在 Outlook 中打印帶圖片的聯繫人
通常,在Outlook中打印聯繫人時,不會將其圖片打印出來。 但是有時,打印帶有圖片的聯繫人會給人留下深刻的印象。 本文將介紹一些解決方法來完成它。
在 Outlook 中打印電子郵件的選擇
如果您收到一封電子郵件,並且發現需要打印一些電子郵件內容,而不是打印整個郵件,您將怎麼辦? 實際上,Outlook可以藉助Internet瀏覽器(例如Firefox和Internet Explorer)幫助您實現此操作。 在這裡,我將以Internet瀏覽器為例。 請查看以下教程。
最佳辦公生產力工具
Kutools for Outlook - 超過 100 種強大的功能可增強您的 Outlook
🤖 人工智慧郵件助手: 具備人工智慧魔力的即時專業電子郵件——一鍵天才回覆、完美語調、多語言掌握。輕鬆改變電子郵件! ……
📧 電子郵件自動化: 外出(適用於 POP 和 IMAP) / 安排發送電子郵件 / 發送電子郵件時按規則自動抄送/密件副本 / 自動轉送(進階規則) / 自動添加問候語 / 自動將多收件者電子郵件拆分為單獨的訊息 ...
📨 電子郵件管理: 輕鬆回憶電子郵件 / 按主題和其他人阻止詐騙電子郵件 / 刪除重複的電子郵件 / 進階搜索 / 合併資料夾 ...
📁 附件專業版: 批量保存 / 批量分離 / 批量壓縮 / 自動保存 / 自動分離 / 自動壓縮 ...
🌟 介面魔法: 😊更多又漂亮又酷的表情符號 / 使用選項卡式視圖提高 Outlook 工作效率 / 最小化 Outlook 而不是關閉 ...
👍 一鍵奇蹟: 使用傳入附件回覆全部 / 反網路釣魚電子郵件 / 🕘顯示寄件者的時區 ...
👩🏼🤝👩🏻 通訊錄和行事曆: 從選定的電子郵件中大量新增聯絡人 / 將聯絡人群組拆分為各組 / 刪除生日提醒 ...
超過 100特點 等待您的探索! 按此處了解更多。