Skip to main content

How to automatically print attachments when emails arrive in Outlook?

Author: Siluvia Last Modified: 2023-02-23

This tutorial demonstrates a method to combine a VBA script and an Outlook rule to help you automatically print attachments of certain emails when they arrive in Outlook.


Automatically print attachments when certain emails arrive

Supposing, you want to print attachments of incoming emails from a certain sender automatically. You can do as follows to get it done.

Step1: Create a script in Outlook

Firstly, you need to create a VBA script in Outlook.

1. Launch your Outlook, press the Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, double click on Project1 > Microsoft Outlook Objects > ThisOutlookSession to open the ThisOutlookSession (Code) window, and then copy the following code into this code window.

VBA code 1: Automatically print attachments (all types of attachments) when emails arrive

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

Note: This code supports printing of all types of attachments received in emails. If you want to print only the specified type of attachment, such as pdf files, please apply the following VBA code.

VBA code 2: Automatically print the specified type of attachments when emails arrive

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

Notes:

1. Before applying this VBA code to print only the pdf file in the incoming emails, first you need to download and install Adobe Acrobat Reader and set it as the default pdf reader in your computer.
2. In the line Case "pdf", please change "pdf" to the file extension you want to print.

3. Go ahead and click Tools > References. In the popping up References – Project1 dialog box, check the Microsoft Scripting Runtime box, and then click the OK button.

4. Save the code and press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

Note: Please make sure that the Enable all macros option is enabled in your Outlook. You can check this option by following the steps shown below.

Step2: Build a rule to use the script

After adding the VBA script in Outlook, you need to create a rule to use the script based on certain conditions.

1. Go to the Home tab, click Rules > Manage Rules & Alerts.

2. In the Rules and Alerts dialog box, click the New Rule button to create a rule.

Tips: If you have added multiple email accounts to your Outlook, please specify an account in the Apply changes to this folder drop-down list where you want to apply the rule. Otherwise, it will be applied to the inbox of the currently selected email account.

3. In the first Rules Wizard dialog box, select Apply rule on messages I receive in the Step 1 box, and then click Next.

4. In the second Rules Wizard dialog box, you need to:

4.1) Specify one or more conditions in the Step 1 box according to your needs;
In this case, I want to print only the attachments in incoming emails from a specified sender. Here, I check the from people or public group box.
4.2) Click the underlined value in the Step 2 box to edit the condition;
4.3) Click Next. See screenshot:

5. In the third Rules Wizard dialog box, you need to configure as follows.

5.1) In the Step 1: Select action(s) section, check the run a script box;
5.2) In the Step 2 section, click the underlined text “a script”;
5.3) In the opening Select Script dialog box, click the name of the VBA code you added above, and then click OK;
5.4) Click the Next button. See screenshot:

Tips: 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.

6. Then another Rules Wizard pops up asking for exceptions. You can select the exceptions if necessary, otherwise, click the Next button without any selections。

7. In the last Rules Wizard, you need to specify a name for the rule, and then click the Finish button.

8. Then it returns to the Rules and Alerts dialog box, you can see the rule you created listed inside, click the OK button to finish the whole settings.

From now on, when an email from the specified person is received, the attached files will be printed automatically.


Related articles

Only Print Attachment(S) From One Email Or Selected Emails In Outlook
In Outlook, you can print the emails, but have you printed the attachments only from one email or selected emails in Outlook? This article introduces the tricks on solving this job.

Only Print Message Header Of An Email In Outlook
When printing an email in Outlook, it will print both of message header and message body in the email. However, in some special cases, you may just need to print out the message header with the subject, sender, recipients, etc. This article will introduce two solutions to do it.

Print A Calendar In A Specified/Custom Date Range In Outlook
Normally, when printing a calendar in Month view in Outlook, it will automatically select the month containing the currently selected date. But, you may need to print the calendar within a custom date range, such as 3 months, half of year, etc. This article will introduce the solution for you.

Print A Contact With Picture In Outlook
Normally, a contact’s picture won’t be printed out when printing the contact in Outlook. But sometimes, it will be more impressive to print a contact with its picture. This article will introduce some workarounds to get it done.

Print A Selection Of An Email In Outlook
If you received an email message and found that there is a selection of the email content need to be printed out instead of printing the whole message, what would you do? Actually, Outlook can help you achieve this operation with the help of internet browsers, such as the Firefox and the Internet Explorer. Here I will take the Internet browsers for example. Please look at the following tutorials.

More articles about "printing in Outlook"...


Best Office Productivity Tools

Breaking News: Kutools for Outlook Launches Free Version!

Experience the all-new Kutools for Outlook FREE version with 70+ incredible features, yours to use FOREVER! Click to download now!

🤖 Kutools AI : Instant pro emails with AI magic--one-click to genius replies, perfect tone, multilingual mastery. Transform emailing effortlessly! ...

📧 Email Automation: Auto Reply (Available for POP and IMAP)  /  Schedule Send Emails  /  Auto CC/BCC by Rules When Sending Email  /  Auto Forward (Advanced Rules)   /  Auto Add Greeting   /  Automatically Split Multi-Recipient Emails into Individual Messages ...

📨 Email Management: Recall Emails  /  Block Scam Emails by Subjects and Others  /  Delete Duplicate Emails  /  Advanced Search  /  Consolidate Folders ...

📁 Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface Magic: 😊More Pretty and Cool Emojis   /  Remind you when important emails come  /  Minimize Outlook Instead of Closing ...

👍 One-click Wonders: Reply All with Incoming Attachments  /   Anti-Phishing Emails  /  🕘Show Sender's Time Zone ...

👩🏼‍🤝‍👩🏻 Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Instantly unlock Kutools for Outlook with a single click—permanently free. Don't wait, download now and boost your efficiency!

kutools for outlook features1 kutools for outlook features2
 

 

 

Comments (23)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
HI!

When the script print attach picture the "preview printing" starts and you need to take action ant press print. Do you have any solution on this?
Best regards Christofer
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.
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations