Note: The other languages of the website are Google-translated. Back to English

How to automatically print attachments when emails arrive in Outlook?

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 20220413
  Dim xFS As FileSystemObject
  Dim xTempFolder As String
  Dim xAtt As Attachment
  On Error Resume Next
  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, 4))
    xFileName = xTempFolder & "\" & xFileName
    xAtt.SaveAsFile (xFileName)
      Set xFolderItem = xFolder.ParseName(xFileName)
      xFolderItem.InvokeVerbEx ("print")
  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

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 20220413
  Dim xFS As FileSystemObject
  Dim xTempFolder As String
  Dim xAtt As Attachment
  On Error Resume Next
  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, 4))
    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

Note: 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"...


Kutools for Outlook - Brings 100 Advanced Features to Outlook, and Make Work Much Easier!

  • Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by custom; Auto Reply without exchange server, and more automatic features...
  • BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
  • Reply (All) With All Attachments in the mail conversation; Reply Many Emails in seconds; Auto Add Greeting when reply; Add Date into subject...
  • Attachment Tools: Manage All Attachments in All Mails, Auto Detach, Compress All, Rename All, Save All... Quick Report, Count Selected Mails...
  • Powerful Junk Emails by custom; Remove Duplicate Mails and Contacts... Enable you to do smarter, faster and better in Outlook.
shot kutools outlook kutools tab 1180x121
shot kutools outlook kutools plus tab 1180x121
 
Comments (1)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
bonjour

j ai suivi a la lettre votre methode pour imprimez automatiquement lesz pieces jointess

activez les macros mais il n y a pas de macro

donc ca ne fonctionne pas ou il me manque un element

merci de me dire svp
jean louis sueur
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0  Characters
Suggested Locations