Bỏ qua nội dung chính

Làm cách nào để tự động in tệp đính kèm khi email đến trong Outlook?

Hướng dẫn này trình bày phương pháp kết hợp tập lệnh VBA và quy tắc Outlook để giúp bạn tự động in tệp đính kèm của một số email nhất định khi chúng đến trong Outlook.


Tự động in tệp đính kèm khi có một số email nhất định đến

Giả sử, bạn muốn tự động in các tệp đính kèm của email đến từ một người gửi nhất định. Bạn có thể làm như sau để hoàn thành công việc.

Bước 1: Tạo tập lệnh trong Outlook

Trước tiên, bạn cần tạo một tập lệnh VBA trong Outlook.

1. Khởi chạy Outlook của bạn, nhấn Khác + F11 các phím đồng thời để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.

2. bên trong Microsoft Visual Basic cho các ứng dụng cửa sổ, nhấp đúp vào Project1 > Đối tượng Microsoft Outlook > ĐâyOutlookSession để mở ThisOutlookSession (Mã) cửa sổ, và sau đó sao chép mã sau vào cửa sổ mã này.

Mã VBA 1: Tự động in tệp đính kèm (tất cả các loại tệp đính kèm) khi có email

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

Lưu ý: Mã này hỗ trợ in tất cả các loại tệp đính kèm nhận được trong email. Nếu bạn chỉ muốn in loại tệp đính kèm được chỉ định, chẳng hạn như tệp pdf, vui lòng áp dụng mã VBA sau.

Mã VBA 2: Tự động in loại tệp đính kèm được chỉ định khi có email

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

Chú ý:

1. Trước khi áp dụng mã VBA này để chỉ in tệp pdf trong các email đến, trước tiên bạn cần tải xuống và cài đặt Adobe Acrobat Reader và đặt nó làm trình đọc pdf mặc định trong máy tính của bạn.
2. Trong dòng Trường hợp "pdf", hãy thay đổi "pdf" vào phần mở rộng tệp bạn muốn in.

3. Tiếp tục và nhấp vào CÔNG CỤ > Tài liệu tham khảo. Trong cửa sổ bật lên Tài liệu tham khảo - Project1 hộp thoại, kiểm tra Thời gian chạy tập lệnh của Microsoft và sau đó bấm vào OK .

4. Lưu mã và nhấn Khác + Q chìa khóa để đóng Microsoft Visual Basic cho các ứng dụng cửa sổ.

Lưu ý: Hãy chắc chắn rằng Bật tất cả các macro tùy chọn được bật trong Outlook của bạn. Bạn có thể kiểm tra tùy chọn này bằng cách làm theo các bước được hiển thị bên dưới.

Bước 2: Xây dựng quy tắc để sử dụng tập lệnh

Sau khi thêm tập lệnh VBA trong Outlook, bạn cần tạo quy tắc để sử dụng tập lệnh dựa trên các điều kiện nhất định.

1. Chuyển đến tab Trang chủ, nhấp vào Nội quy > Quản lý Quy tắc & Cảnh báo.

2. bên trong Quy tắc và Cảnh báo hộp thoại, nhấp vào Quy tắc mới để tạo quy tắc.

Lời khuyên: Nếu bạn đã thêm nhiều tài khoản email vào Outlook của mình, vui lòng chỉ định một tài khoản trong Áp dụng các thay đổi cho thư mục này danh sách thả xuống nơi bạn muốn áp dụng quy tắc. Nếu không, nó sẽ được áp dụng cho hộp thư đến của tài khoản email hiện được chọn.

3. Trong lần đầu tiên Trình hướng dẫn quy tắc hộp thoại, chọn Áp dụng quy tắc về thư tôi nhận được trong Bước 1 hộp, và sau đó nhấp vào Tiếp theo.

4. Trong giây Trình hướng dẫn quy tắc hộp thoại, bạn cần:

4.1) Chỉ định một hoặc nhiều điều kiện trong Bước 1 hộp theo nhu cầu của bạn;
Trong trường hợp này, tôi chỉ muốn in các tệp đính kèm trong email đến từ một người gửi được chỉ định. Đây, tôi kiểm tra từ mọi người hoặc nhóm công khai cái hộp.
4.2) Nhấp vào giá trị được gạch chân trong Bước 2 hộp để chỉnh sửa điều kiện;
4.3) Nhấp Tiếp theo. Xem ảnh chụp màn hình:

5. Trong phần ba Trình hướng dẫn quy tắc hộp thoại, bạn cần cấu hình như sau.

5.1) Trong Bước 1: Chọn (các) phần hành động, kiểm tra chạy một tập lệnh cái hộp;
5.2) Trong Bước 2 , hãy nhấp vào văn bản được gạch chân “một tập lệnh”;
5.3) Mở đầu Chọn tập lệnh hộp thoại, bấm vào tên của mã VBA bạn đã thêm ở trên, sau đó bấm ĐỒNG Ý;
5.4) Nhấp vào Sau cái nút. Xem ảnh chụp màn hình:

Lời khuyên: Nếu người Vikingchạy một tập lệnh”Tùy chọn bị thiếu trong Trình hướng dẫn quy tắc, bạn có thể hiển thị nó bằng cách làm theo phương pháp được đề cập trong bài viết này: khôi phục mã lệnh Run A Script bị thiếu trong quy tắc Outlook.

6. Sau đó khác Trình hướng dẫn quy tắc bật lên yêu cầu các trường hợp ngoại lệ. Bạn có thể chọn các ngoại lệ nếu cần, nếu không, hãy nhấp vào Sau nút không có bất kỳ lựa chọn nào。

7. Cuối cùng Trình hướng dẫn quy tắc, bạn cần chỉ định tên cho quy tắc, sau đó nhấp vào Kết thúc .

8. Sau đó, nó trở về Quy tắc và Cảnh báo hộp thoại, bạn có thể thấy quy tắc bạn đã tạo được liệt kê bên trong, nhấp vào OK để hoàn tất toàn bộ cài đặt.

Kể từ bây giờ, khi nhận được email từ người được chỉ định, các tệp đính kèm sẽ được in tự động.


Các bài liên quan

Chỉ in (S) tệp đính kèm từ một email hoặc các email đã chọn trong Outlook
Trong Outlook, bạn có thể in các email, nhưng bạn đã in các tệp đính kèm chỉ từ một email hoặc các email đã chọn trong Outlook chưa? Bài viết này giới thiệu các thủ thuật để giải quyết công việc này.

Chỉ in tiêu đề thư của email trong Outlook
Khi in email trong Outlook, nó sẽ in cả tiêu đề thư và nội dung thư trong email. Tuy nhiên, trong một số trường hợp đặc biệt, bạn có thể chỉ cần in tiêu đề thư với chủ đề, người gửi, người nhận,… Bài viết này sẽ giới thiệu hai giải pháp để thực hiện.

In lịch trong phạm vi ngày được chỉ định / tùy chỉnh trong Outlook
Thông thường, khi in lịch ở dạng xem Tháng trong Outlook, nó sẽ tự động chọn tháng có chứa ngày hiện được chọn. Tuy nhiên, bạn có thể cần in lịch trong một phạm vi ngày tùy chỉnh, chẳng hạn như 3 tháng, nửa năm,… Bài viết này sẽ giới thiệu giải pháp cho bạn.

In một liên hệ bằng hình ảnh trong Outlook
Thông thường, ảnh của liên hệ sẽ không được in ra khi in liên hệ trong Outlook. Nhưng đôi khi, sẽ ấn tượng hơn nếu in một liên hệ với hình ảnh của nó. Bài viết này sẽ giới thiệu một số cách giải quyết để thực hiện nó.

In lựa chọn email trong Outlook
Nếu bạn nhận được một email và nhận thấy rằng có một số nội dung email cần được in ra thay vì in toàn bộ email, bạn sẽ làm gì? Trên thực tế, Outlook có thể giúp bạn thực hiện thao tác này với sự trợ giúp của các trình duyệt internet, chẳng hạn như Firefox và Internet Explorer. Ở đây tôi sẽ lấy các trình duyệt Internet làm ví dụ. Hãy xem các hướng dẫn sau đây.

Các bài viết khác về "in trong Outlook" ...


Công cụ năng suất văn phòng tốt nhất

Kutools cho Outlook - Hơn 100 tính năng mạnh mẽ để tăng cường Outlook của bạn

🤖 Trợ lý thư AI: Email chuyên nghiệp tức thì với phép thuật AI--một cú nhấp chuột để có câu trả lời xuất sắc, giọng điệu hoàn hảo, khả năng thông thạo đa ngôn ngữ. Chuyển đổi email một cách dễ dàng! ...

📧 Tự động hoá email: Vắng Mặt (Có sẵn cho POP và IMAP)  /  Lên lịch gửi email  /  Tự động CC/BCC theo quy định khi gửi Email  /  Tự động chuyển tiếp (Quy tắc nâng cao)   /  Tự động thêm lời chào   /  Tự động chia email nhiều người nhận thành các tin nhắn riêng lẻ hữu ích. Cảm ơn !

📨 Quản lý email: Dễ dàng thu hồi email  /  Chặn email lừa đảo theo chủ đề và những người khác  /  Xóa các email trùng lặp  /  Tìm Kiếm Nâng Cao  /  Hợp nhất các thư mục hữu ích. Cảm ơn !

📁 Tệp đính kèm chuyên nghiệpLưu hàng loạt  /  Tách hàng loạt  /  Nén hàng loạt  /  Tự động lưu   /  Tự động tách  /  Tự động nén hữu ích. Cảm ơn !

🌟 Giao diện ma thuật: 😊Thêm nhiều biểu tượng cảm xúc đẹp và thú vị hơn   /  Tăng năng suất Outlook của bạn với chế độ xem theo thẻ  /  Thu nhỏ Outlook thay vì đóng hữu ích. Cảm ơn !

👍 Kỳ quan chỉ bằng một cú nhấp chuột: Trả lời tất cả bằng tệp đính kèm đến  /   Email chống lừa đảo  /  🕘Hiển thị múi giờ của người gửi hữu ích. Cảm ơn !

👩🏼‍🤝‍👩🏻 Danh bạ & Lịch: Thêm hàng loạt liên hệ từ các email đã chọn  /  Chia nhóm liên hệ thành các nhóm riêng lẻ  /  Xóa lời nhắc sinh nhật hữu ích. Cảm ơn !

Trên 100 tính năng Chờ đợi sự khám phá của bạn! Bấm vào đây để khám phá thêm.

 

 

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