Vai al contenuto principale

Come stampare automaticamente gli allegati quando arrivano le e-mail in Outlook?

Questo tutorial mostra un metodo per combinare uno script VBA e una regola di Outlook per aiutarti a stampare automaticamente gli allegati di determinate e-mail quando arrivano in Outlook.


Stampa automaticamente gli allegati quando arrivano determinate e-mail

Supponiamo di voler stampare automaticamente gli allegati delle e-mail in arrivo da un determinato mittente. Puoi fare come segue per farlo.

Passaggio 1: crea uno script in Outlook

Innanzitutto, devi creare uno script VBA in Outlook.

1. Avvia Outlook, premi il pulsante altro + F11 tasti contemporaneamente per aprire il file Microsoft Visual Basic, Applications Edition finestra.

2. Nel Microsoft Visual Basic, Applications Edition finestra, fare doppio clic su Project1 > Oggetti di Microsoft Outlook > Questa sessione di Outlook per aprire il ThisOutlookSession (codice) finestra e quindi copiare il codice seguente in questa finestra di codice.

Codice VBA 1: stampa automaticamente gli allegati (tutti i tipi di allegati) quando arrivano le e-mail

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

Nota: Questo codice supporta la stampa di tutti i tipi di allegati ricevuti nelle e-mail. Se desideri stampare solo il tipo di allegato specificato, ad esempio file pdf, applica il seguente codice VBA.

Codice VBA 2: stampa automaticamente il tipo di allegati specificato all'arrivo delle e-mail

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

Note:

1. Prima di applicare questo codice VBA per stampare solo il file pdf nelle e-mail in arrivo, devi prima scaricare e installare Adobe Acrobat Reader e impostalo come lettore pdf predefinito nel tuo computer.
2. In linea Caso "pdf", per favore cambia "PDF" all'estensione del file che si desidera stampare.

3. Vai avanti e fai clic Strumenti > Riferimenti. Nel spuntare Riferimenti – Progetto1 finestra di dialogo, selezionare Runtime di script Microsoft casella, quindi fare clic su OK pulsante.

4. Salvare il codice e premere il tasto altro + Q i tasti per chiudere il file Microsoft Visual Basic, Applications Edition finestra.

Nota: Si prega di assicurarsi che il Abilita tutte le macro opzione è abilitata in Outlook. Puoi controllare questa opzione seguendo i passaggi mostrati di seguito.

Passaggio 2: crea una regola per utilizzare lo script

Dopo aver aggiunto lo script VBA in Outlook, è necessario creare una regola per utilizzare lo script in base a determinate condizioni.

1. Vai alla scheda Home, fai clic su Regole > Gestisci regole e avvisi.

2. Nel Regole e avvisi finestra di dialogo, fare clic su Nuova regola pulsante per creare una regola.

Suggerimenti: Se hai aggiunto più account e-mail a Outlook, specifica un account nel file Applica le modifiche a questa cartella elenco a discesa in cui si desidera applicare la regola. In caso contrario, verrà applicato alla posta in arrivo dell'account e-mail attualmente selezionato.

3. Nella prima Creazione guidata regole finestra di dialogo, selezionare Applica la regola sui messaggi che ricevo nel Passo 1 casella, quindi fare clic Avanti.

4. Nella seconda Creazione guidata regole finestra di dialogo, è necessario:

4.1) Specificare una o più condizioni nella Passo 1 scatola secondo le vostre esigenze;
In questo caso, voglio stampare solo gli allegati nelle e-mail in arrivo da un mittente specificato. Qui, controllo il da persone o gruppi pubblici scatola.
4.2) Fare clic sul valore sottolineato in Passo 2 casella per modificare la condizione;
4.3) Fare clic Avanti. Visualizza gli screenshot:

5. Nella terza Creazione guidata regole finestra di dialogo, è necessario configurare come segue.

5.1) nel Passaggio 1: seleziona la sezione delle azioni, controlla il eseguire uno script scatola;
5.2) nel Passo 2 sezione, fare clic sul testo sottolineato “uno script”;
5.3) In apertura Seleziona Script finestra di dialogo, fare clic sul nome del codice VBA aggiunto in precedenza, quindi fare clic OK;
5.4) Fare clic su Successivo pulsante. Vedi screenshot:

Suggerimenti: Se la "eseguire uno scriptL'opzione "manca nel tuo Creazione guidata regole, puoi visualizzarlo seguendo il metodo menzionato in questo articolo: ripristino mancante Esegui una pption di script nella regola di Outlook.

6. Poi un altro Creazione guidata regole si apre chiedendo eccezioni. È possibile selezionare le eccezioni se necessario, altrimenti fare clic su Successivo pulsante senza alcuna selezione。

7. Nell'ultimo Creazione guidata regole, è necessario specificare un nome per la regola, quindi fare clic su Fine pulsante.

8. Quindi ritorna al file Regole e avvisi finestra di dialogo, puoi vedere la regola che hai creato elencata all'interno, fai clic su OK pulsante per completare tutte le impostazioni.

D'ora in poi, quando viene ricevuta un'e-mail dalla persona specificata, i file allegati verranno stampati automaticamente.


Articoli correlati

Stampa solo allegati da un'e-mail o da e-mail selezionate in Outlook
In Outlook, puoi stampare le e-mail, ma hai stampato gli allegati solo da un'e-mail o da e-mail selezionate in Outlook? Questo articolo introduce i trucchi per risolvere questo lavoro.

Stampa solo l'intestazione del messaggio di un'e-mail in Outlook
Quando si stampa un'e-mail in Outlook, verranno stampate sia l'intestazione del messaggio che il corpo del messaggio nell'e-mail. Tuttavia, in alcuni casi speciali, potrebbe essere sufficiente stampare l'intestazione del messaggio con l'oggetto, il mittente, i destinatari, ecc. Questo articolo introdurrà due soluzioni per farlo.

Stampa un calendario in un intervallo di date specifico/personalizzato in Outlook
Normalmente, quando si stampa un calendario nella visualizzazione Mese in Outlook, verrà selezionato automaticamente il mese contenente la data attualmente selezionata. Tuttavia, potrebbe essere necessario stampare il calendario all'interno di un intervallo di date personalizzato, ad esempio 3 mesi, metà dell'anno, ecc. Questo articolo introdurrà la soluzione per te.

Stampa un contatto con un'immagine in Outlook
Normalmente, l'immagine di un contatto non verrà stampata durante la stampa del contatto in Outlook. Ma a volte sarà più impressionante stampare un contatto con la sua immagine. Questo articolo introdurrà alcune soluzioni alternative per farlo.

Stampa una selezione di un'e-mail in Outlook
Se hai ricevuto un messaggio e-mail e hai scoperto che è necessario stampare una selezione del contenuto dell'e-mail invece di stampare l'intero messaggio, cosa faresti? In realtà, Outlook può aiutarti a realizzare questa operazione con l'aiuto dei browser Internet, come Firefox e Internet Explorer. Qui prenderò ad esempio i browser Internet. Si prega di guardare i seguenti tutorial.

Altri articoli sulla "stampa in Outlook"...


I migliori strumenti per la produttività in ufficio

Kutools for Outlook - Oltre 100 potenti funzionalità per potenziare il tuo Outlook

🤖 Assistente di posta AI: E-mail istantanee professionali con la magia dell'intelligenza artificiale: risposte geniali con un solo clic, tono perfetto, padronanza multilingue. Trasforma l'e-mail senza sforzo! ...

???? Automazione di posta elettronica: Fuori sede (disponibile per POP e IMAP)  /  Pianifica l'invio di e-mail  /  CC/BCC automatico in base alle regole durante l'invio di e-mail  /  Inoltro automatico (regole avanzate)   /  Aggiunta automatica di saluto   /  Suddividi automaticamente le email con più destinatari in singoli messaggi ...

📨 gestione e-mail: Richiama facilmente le email  /  Blocca le email truffe per soggetto e altri  /  Elimina email duplicate  /  Ricerca avanzata  /  Consolidare cartelle ...

📁 Allegati ProSalvataggio in batch  /  Stacca batch  /  Comprimi in lotti  /  Salvataggio automatico   /  Scollegamento automatico  /  Comprimi automaticamente ...

🌟 Magia dell'interfaccia: 😊Più emoji carini e fantastici   /  Aumenta la tua produttività in Outlook con le visualizzazioni a schede  /  Riduci a icona Outlook invece di chiuderlo ...

👍 Meraviglie con un clic: Rispondi a tutti con gli allegati in arrivo  /   E-mail anti-phishing  /  🕘Mostra il fuso orario del mittente ...

👩🏼‍🤝‍👩🏻 Contatti e calendario: Aggiungi in batch contatti dalle email selezionate  /  Dividere un gruppo di contatti in singoli gruppi  /  Rimuovi promemoria compleanno ...

Al di sopra Caratteristiche 100 Attendi la tua esplorazione! Clicca qui per scoprire di più.

 

 

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