ไปยังเนื้อหาหลัก

วิธีพิมพ์เอกสารแนบโดยอัตโนมัติเมื่ออีเมลมาถึง Outlook?

บทช่วยสอนนี้สาธิตวิธีการรวมสคริปต์ VBA และกฎของ Outlook เพื่อช่วยให้คุณพิมพ์เอกสารแนบของอีเมลบางฉบับโดยอัตโนมัติเมื่อมาถึง Outlook


พิมพ์เอกสารแนบโดยอัตโนมัติเมื่อมีอีเมลบางฉบับมาถึง

สมมติว่า คุณต้องการพิมพ์เอกสารแนบของอีเมลขาเข้าจากผู้ส่งบางรายโดยอัตโนมัติ คุณสามารถทำได้ดังนี้

ขั้นที่ 1: สร้างสคริปต์ใน Outlook

ประการแรก คุณต้องสร้างสคริปต์ VBA ใน Outlook

1. เปิดโปรแกรม Outlook ของคุณกดปุ่ม อื่น ๆ + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2 ใน Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง ดับเบิลคลิกที่ Project1 > วัตถุ Microsoft Outlook > นี้OutlookSession เพื่อเปิด 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. ไปข้างหน้าและคลิก เครื่องมือ > อ้างอิง ในการโผล่ขึ้นมา ข้อมูลอ้างอิง – Project1 ให้ทำเครื่องหมายที่ รันไทม์การเขียนสคริปต์ของ Microsoft จากนั้นคลิกที่ไฟล์ OK ปุ่ม

4. บันทึกรหัสแล้วกดปุ่ม อื่น ๆ + Q ปุ่มเพื่อปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

หมายเหตุ โปรดตรวจสอบให้แน่ใจว่า เปิดใช้งานมาโครทั้งหมด เปิดใช้งานตัวเลือกใน Outlook ของคุณ คุณสามารถตรวจสอบตัวเลือกนี้โดยทำตามขั้นตอนที่แสดงด้านล่าง

ขั้นตอนที่ 2: สร้างกฎเพื่อใช้สคริปต์

หลังจากเพิ่มสคริปต์ VBA ใน Outlook แล้ว คุณต้องสร้างกฎเพื่อใช้สคริปต์ตามเงื่อนไขบางประการ

1. ไปที่แท็บหน้าแรก คลิก กฎระเบียบ > จัดการกฎและการแจ้งเตือน.

2 ใน กฎและการแจ้งเตือน ใหคลิกปุ the ม กฎใหม่ ปุ่มเพื่อสร้างกฎ

ทิปส์: หากคุณได้เพิ่มบัญชีอีเมลหลายบัญชีใน Outlook ของคุณ โปรดระบุบัญชีใน ใช้การเปลี่ยนแปลงกับโฟลเดอร์นี้ รายการแบบหล่นลงที่คุณต้องการใช้กฎ มิฉะนั้น จะนำไปใช้กับกล่องจดหมายของบัญชีอีเมลที่เลือกในปัจจุบัน

3. ในครั้งแรก ตัวช่วยสร้างกฎ ใหเลือก ใช้กฎเกี่ยวกับข้อความที่ฉันได้รับ ใน ขั้นตอนที่ 1 จากนั้นคลิก ถัดไป

4. ในวินาทีที่ ตัวช่วยสร้างกฎ คุณต้อง:

4.1) ระบุอย่างน้อยหนึ่งเงื่อนไขใน ขั้นตอนที่ 1 กล่องตามความต้องการของคุณ
ในกรณีนี้ ฉันต้องการพิมพ์เฉพาะไฟล์แนบในอีเมลขาเข้าจากผู้ส่งที่ระบุ ที่นี่ฉันตรวจสอบ จากบุคคลหรือกลุ่มสาธารณะ กล่อง.
4.2) คลิกค่าที่ขีดเส้นใต้ใน ขั้นตอนที่ 2 กล่องสำหรับแก้ไขเงื่อนไข
4.3) คลิก ถัดไป ดูภาพหน้าจอ:

5. ในข้อที่สาม ตัวช่วยสร้างกฎ คุณต้องกำหนดค่าดังต่อไปนี้

5.1) ใน ขั้นตอนที่ 1: เลือกส่วนการดำเนินการตรวจสอบไฟล์ เรียกใช้สคริปต์ กล่อง;
5.2) ใน ขั้นตอนที่ 2 คลิกที่ข้อความที่ขีดเส้นใต้ "สคริปต์";
5.3) ในการเปิด เลือกสคริปต์ กล่องโต้ตอบ คลิกชื่อของโค้ด VBA ที่คุณเพิ่มด้านบน จากนั้นคลิก ตกลง;
5.4) คลิกปุ่ม ถัดไป ปุ่ม. ดูภาพหน้าจอ:

ทิปส์: หาก "เรียกใช้สคริปต์” ตัวเลือกหายไปในของคุณ ตัวช่วยสร้างกฎคุณสามารถแสดงได้โดยทำตามวิธีการที่กล่าวถึงในบทความนี้: กู้คืน Run A Script pption ที่ขาดหายไปในกฎของ Outlook.

6. แล้วอีกอย่าง ตัวช่วยสร้างกฎ ปรากฏขึ้นเพื่อขอข้อยกเว้น คุณสามารถเลือกข้อยกเว้นได้หากจำเป็น หรือมิฉะนั้น ให้คลิกปุ่ม ถัดไป ปุ่มโดยไม่ต้องเลือกใดๆ。

7. ในช่วงสุดท้าย ตัวช่วยสร้างกฎคุณต้องระบุชื่อสำหรับกฎ จากนั้นคลิก เสร็จสิ้น ปุ่ม

8. จากนั้นจะกลับไปที่ กฎและการแจ้งเตือน ไดอะล็อกบ็อกซ์ คุณสามารถดูกฎที่คุณสร้างอยู่ในรายการ คลิก OK เพื่อสิ้นสุดการตั้งค่าทั้งหมด

จากนี้ไปเมื่อได้รับอีเมลจากบุคคลที่ระบุ ไฟล์ที่แนบมาจะถูกพิมพ์โดยอัตโนมัติ


บทความที่เกี่ยวข้อง

พิมพ์เฉพาะไฟล์แนบจากอีเมลหนึ่งฉบับหรืออีเมลที่เลือกใน Outlook
ใน Outlook คุณสามารถพิมพ์อีเมลได้ แต่คุณได้พิมพ์เอกสารแนบจากอีเมลฉบับเดียวหรืออีเมลที่เลือกใน Outlook หรือไม่ บทความนี้จะแนะนำเคล็ดลับในการแก้ปัญหานี้

พิมพ์เฉพาะส่วนหัวของอีเมลใน Outlook
เมื่อพิมพ์อีเมลใน Outlook จะพิมพ์ทั้งส่วนหัวและเนื้อหาของข้อความในอีเมล อย่างไรก็ตาม ในบางกรณีพิเศษ คุณอาจต้องพิมพ์ส่วนหัวของข้อความที่มีหัวเรื่อง ผู้ส่ง ผู้รับ ฯลฯ บทความนี้จะแนะนำวิธีแก้ปัญหาสองวิธี

พิมพ์ปฏิทินในช่วงวันที่ที่ระบุ/กำหนดเองใน Outlook
โดยปกติ เมื่อพิมพ์ปฏิทินในมุมมองเดือนใน Outlook ปฏิทินจะเลือกเดือนที่มีวันที่ที่เลือกในปัจจุบันโดยอัตโนมัติ แต่คุณอาจต้องพิมพ์ปฏิทินภายในช่วงวันที่ที่กำหนดเอง เช่น 3 เดือน ครึ่งปี เป็นต้น บทความนี้จะแนะนำวิธีแก้ปัญหาสำหรับคุณ

พิมพ์ผู้ติดต่อพร้อมรูปภาพใน Outlook
โดยปกติรูปภาพของผู้ติดต่อจะไม่ถูกพิมพ์ออกมาเมื่อพิมพ์ผู้ติดต่อใน Outlook แต่บางครั้งการพิมพ์รายชื่อติดต่อด้วยรูปภาพจะน่าประทับใจกว่า บทความนี้จะแนะนำวิธีแก้ปัญหาบางอย่างเพื่อทำให้เสร็จ

พิมพ์การเลือกอีเมลใน Outlook
หากคุณได้รับข้อความอีเมลและพบว่ามีเนื้อหาอีเมลบางส่วนที่ต้องพิมพ์ออกมาแทนที่จะพิมพ์ข้อความทั้งหมดคุณจะทำอย่างไร? จริงๆแล้ว Outlook สามารถช่วยให้คุณดำเนินการนี้ได้ด้วยความช่วยเหลือของอินเทอร์เน็ตเบราว์เซอร์เช่น Firefox และ Internet Explorer ที่นี่ฉันจะยกตัวอย่างอินเทอร์เน็ตเบราว์เซอร์ โปรดดูบทแนะนำต่อไปนี้

บทความเพิ่มเติมเกี่ยวกับ "การพิมพ์ใน Outlook"...


สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

Kutools สำหรับ Outlook - คุณสมบัติอันทรงพลังมากกว่า 100 รายการเพื่อเติมพลังให้กับ Outlook ของคุณ

🤖 ผู้ช่วยจดหมาย AI: ส่งอีเมลระดับมืออาชีพทันทีด้วยเวทมนตร์ AI คลิกเพียงครั้งเดียวเพื่อตอบกลับอย่างชาญฉลาด น้ำเสียงที่สมบูรณ์แบบ การเรียนรู้หลายภาษา เปลี่ยนรูปแบบการส่งอีเมลอย่างง่ายดาย! ...

📧 การทำงานอัตโนมัติของอีเมล: ไม่อยู่ที่สำนักงาน (ใช้ได้กับ POP และ IMAP)  /  กำหนดการส่งอีเมล  /  Auto CC/BCC ตามกฎเมื่อส่งอีเมล  /  ส่งต่ออัตโนมัติ (กฎขั้นสูง)   /  เพิ่มคำทักทายอัตโนมัติ   /  แบ่งอีเมลผู้รับหลายรายออกเป็นข้อความส่วนตัวโดยอัตโนมัติ ...

📨 การจัดการอีเมล์: เรียกคืนอีเมลได้อย่างง่ายดาย  /  บล็อกอีเมลหลอกลวงตามหัวเรื่องและอื่นๆ  /  ลบอีเมลที่ซ้ำกัน  /  การค้นหาขั้นสูง  /  รวมโฟลเดอร์ ...

📁 ไฟล์แนบโปรบันทึกแบทช์  /  การแยกแบทช์  /  การบีบอัดแบบแบตช์  /  บันทึกอัตโนมัติ   /  ถอดอัตโนมัติ  /  บีบอัดอัตโนมัติ ...

🌟 อินเตอร์เฟซเมจิก: 😊อีโมจิที่สวยและเจ๋งยิ่งขึ้น   /  เพิ่มประสิทธิภาพการทำงาน 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