Ga naar hoofdinhoud

Hoe automatisch bijlagen afdrukken wanneer e-mails in Outlook aankomen?

Deze zelfstudie demonstreert een methode om een ​​VBA-script en een Outlook-regel te combineren om u te helpen automatisch bijlagen van bepaalde e-mails af te drukken wanneer ze in Outlook aankomen.


Automatisch bijlagen afdrukken wanneer bepaalde e-mails binnenkomen

Stel dat u bijlagen van inkomende e-mails van een bepaalde afzender automatisch wilt afdrukken. Je kunt het als volgt doen om het voor elkaar te krijgen.

Stap 1: Maak een script in Outlook

Allereerst moet u een VBA-script maken in Outlook.

1. Start uw Outlook, druk op de anders + F11 toetsen tegelijkertijd om het Microsoft Visual Basic voor toepassingen venster.

2. In de Microsoft Visual Basic voor toepassingen venster, dubbelklik op Project1 > Microsoft Outlook-objecten > DezeOutlookSessie om de te openen ThisOutlookSession (code) venster en kopieer vervolgens de volgende code naar dit codevenster.

VBA-code 1: automatisch bijlagen afdrukken (alle soorten bijlagen) wanneer e-mails binnenkomen

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

Opmerking: Deze code ondersteunt het afdrukken van alle soorten bijlagen die in e-mails zijn ontvangen. Als u alleen het opgegeven type bijlage wilt afdrukken, zoals pdf-bestanden, past u de volgende VBA-code toe.

VBA-code 2: Druk automatisch het opgegeven type bijlagen af ​​wanneer e-mails binnenkomen

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

Opmerkingen:

1. Voordat u deze VBA-code toepast om alleen het pdf-bestand in de inkomende e-mails af te drukken, moet u eerst downloaden en installeren Adobe Acrobat Reader en stel het in als de standaard pdf-lezer op uw computer.
2. In de rij Case "pdf", verander alsjeblieft "pdf" naar de bestandsextensie die u wilt afdrukken.

3. Ga je gang en klik op Tools > Referenties. Bij het opduiken Referenties – Project1 dialoogvenster, controleer de Microsoft Scripting-runtime vak en klik vervolgens op het OK knop.

4. Sla de code op en druk op anders + Q toetsen om de Microsoft Visual Basic voor toepassingen venster.

Opmerking: Zorg ervoor dat de Schakel alle macro's in optie is ingeschakeld in uw Outlook. U kunt deze optie aanvinken door de onderstaande stappen te volgen.

Stap 2: Bouw een regel om het script te gebruiken

Nadat u het VBA-script in Outlook hebt toegevoegd, moet u een regel maken om het script te gebruiken op basis van bepaalde voorwaarden.

1. Ga naar het tabblad Home, klik op Reglement > Beheer regels en waarschuwingen.

2. In de Regels en waarschuwingen dialoogvenster, klik op de Nieuwe regel knop om een ​​regel te maken.

Tips: Als u meerdere e-mailaccounts aan uw Outlook heeft toegevoegd, geef dan een account op in de Wijzigingen toepassen op deze map vervolgkeuzelijst waar u de regel wilt toepassen. Anders wordt het toegepast op de inbox van het momenteel geselecteerde e-mailaccount.

3. In de eerste Regels Wizard dialoogvenster, selecteer Regel toepassen op berichten die ik ontvang in de Stap 1 in en klik vervolgens op Next.

4. In de tweede Regels Wizard dialoogvenster, moet u:

4.1) Specificeer een of meer voorwaarden in de Stap 1 doos volgens uw behoeften;
In dit geval wil ik alleen de bijlagen in inkomende e-mails van een opgegeven afzender afdrukken. Hier controleer ik de van mensen of openbare groepen doos.
4.2) Klik op de onderstreepte waarde in de Stap 2 vak om de voorwaarde te bewerken;
4.3) Klik op Next. Zie screenshot:

5. In de derde Regels Wizard dialoogvenster, moet u als volgt configureren.

5.1) In de Stap 1: Selecteer actie(s) sectie, controleer de voer een script uit doos;
5.2) In de Stap 2 sectie, klik op de onderstreepte tekst "een script";
5.3) In de opening Selecteer Script dialoogvenster, klik op de naam van de VBA-code die u hierboven hebt toegevoegd en klik vervolgens op OK;
5.4) Klik op de Volgende knop. Zie screenshot:

Tips: Als de "voer een script uit” optie ontbreekt in uw Regels Wizard, kunt u het weergeven door de methode te volgen die in dit artikel wordt genoemd: herstel ontbrekende Run A Script-optie in Outlook-regel.

6. Dan nog een Regels Wizard verschijnt en vraagt ​​om uitzonderingen. U kunt de uitzonderingen indien nodig selecteren, anders klikt u op de Volgende knop zonder selecties。

7. In de laatste Regels Wizard, moet u een naam voor de regel opgeven en vervolgens op de Finish knop.

8. Daarna keert het terug naar de Regels en waarschuwingen dialoogvenster, kunt u de regel die u hebt gemaakt in de lijst zien, klik op de OK knop om de hele instellingen te voltooien.

Vanaf nu, wanneer een e-mail van de opgegeven persoon wordt ontvangen, worden de bijgevoegde bestanden automatisch afgedrukt.


Gerelateerde artikelen

Alleen bijlage(n) afdrukken vanuit één e-mail of geselecteerde e-mails in Outlook
In Outlook kunt u de e-mails afdrukken, maar hebt u de bijlagen alleen vanuit één e-mail of geselecteerde e-mails in Outlook afgedrukt? Dit artikel introduceert de trucs voor het oplossen van deze taak.

Alleen de berichtkop van een e-mail afdrukken in Outlook
Bij het afdrukken van een e-mail in Outlook, worden zowel de berichtkop als de berichttekst in de e-mail afgedrukt. In sommige speciale gevallen hoeft u echter alleen de berichtkop af te drukken met het onderwerp, de afzender, de ontvangers, enz. Dit artikel introduceert twee oplossingen om dit te doen.

Een kalender afdrukken in een gespecificeerd/aangepast datumbereik in Outlook
Normaal gesproken wordt bij het afdrukken van een kalender in de maandweergave in Outlook automatisch de maand geselecteerd die de momenteel geselecteerde datum bevat. Maar het kan zijn dat u de kalender binnen een aangepast datumbereik moet afdrukken, zoals 3 maanden, een half jaar, enz. Dit artikel introduceert de oplossing voor u.

Een contact met afbeelding afdrukken in Outlook
Normaal gesproken wordt de foto van een contactpersoon niet afgedrukt bij het afdrukken van de contactpersoon in Outlook. Maar soms is het indrukwekkender om een ​​contact met zijn foto af te drukken. Dit artikel introduceert enkele tijdelijke oplossingen om dit voor elkaar te krijgen.

Een selectie van een e-mail afdrukken in Outlook
Als u een e-mailbericht heeft ontvangen en ontdekt dat er een selectie is van de e-mailinhoud die moet worden afgedrukt in plaats van het hele bericht af te drukken, wat zou u dan doen? Eigenlijk kan Outlook u hierbij helpen met behulp van internetbrowsers, zoals Firefox en Internet Explorer. Hier zal ik bijvoorbeeld de internetbrowsers nemen. Bekijk de volgende tutorials.

Meer artikelen over "afdrukken in Outlook"...


Beste Office-productiviteitstools

Kutools for Outlook - Meer dan 100 krachtige functies om uw Outlook een boost te geven

🤖 AI Mail-assistent: Directe professionele e-mails met AI-magie: met één klik geniale antwoorden, perfecte toon, meertalige beheersing. Transformeer e-mailen moeiteloos! ...

???? Email Automation: Niet aanwezig (beschikbaar voor POP en IMAP)  /  Plan het verzenden van e-mails  /  Automatische CC/BCC volgens regels bij het verzenden van e-mail  /  Automatisch doorsturen (geavanceerde regels)   /  Begroeting automatisch toevoegen   /  Splits e-mails van meerdere ontvangers automatisch op in individuele berichten ...

📨 email management: Gemakkelijk e-mails herinneren  /  Blokkeer zwendel-e-mails van onderwerpen en anderen  /  Verwijder dubbele e-mails  /  Uitgebreid Zoeken  /  Consolideer mappen ...

📁 Bijlagen ProBatch opslaan  /  Batch losmaken  /  Batchcompressie  /  Automatisch opslaan   /  Automatisch loskoppelen  /  Automatisch comprimeren ...

???? Interface-magie: 😊Meer mooie en coole emoji's   /  Verhoog uw Outlook-productiviteit met weergaven met tabbladen  /  Minimaliseer Outlook in plaats van te sluiten ...

???? Wonderen met één klik: Beantwoord iedereen met inkomende bijlagen  /   Antiphishing-e-mails  /  🕘Toon de tijdzone van de afzender ...

👩🏼‍🤝‍👩🏻 Contacten en agenda: Batchcontacten toevoegen uit geselecteerde e-mails  /  Splits een contactgroep in individuele groepen  /  Verwijder verjaardagsherinneringen ...

Over 100 Eigenschappen Wacht op je verkenning! Klik hier om meer te ontdekken.

 

 

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