Loncat ke daftar isi utama

Bagaimana cara mencetak lampiran secara otomatis saat email tiba di Outlook?

Tutorial ini mendemonstrasikan metode untuk menggabungkan skrip VBA dan aturan Outlook untuk membantu Anda mencetak lampiran email tertentu secara otomatis saat mereka tiba di Outlook.


Secara otomatis mencetak lampiran ketika email tertentu tiba

Ibaratnya, Anda ingin mencetak lampiran email yang masuk dari pengirim tertentu secara otomatis. Anda dapat melakukan hal berikut untuk menyelesaikannya.

Langkah1: Buat skrip di Outlook

Pertama, Anda perlu membuat skrip VBA di Outlook.

1. Luncurkan Outlook Anda, tekan lain + F11 tombol secara bersamaan untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Dalam Microsoft Visual Basic untuk Aplikasi jendela, klik dua kali pada Project1 > Objek Microsoft Outlook > Sesi Pandangan ini untuk membuka ThisOutlookSession (Kode) jendela, dan kemudian salin kode berikut ke jendela kode ini.

Kode VBA 1: Secara otomatis mencetak lampiran (semua jenis lampiran) saat email tiba

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

Catatan: Kode ini mendukung pencetakan semua jenis lampiran yang diterima dalam email. Jika Anda ingin mencetak hanya jenis lampiran yang ditentukan, seperti file pdf, harap terapkan kode VBA berikut.

Kode VBA 2: Secara otomatis mencetak jenis lampiran yang ditentukan saat email tiba

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

Catatan:

1. Sebelum menerapkan kode VBA ini untuk mencetak hanya file pdf di email masuk, Anda harus mengunduh dan menginstal terlebih dahulu Adobe Acrobat Reader dan atur sebagai pembaca pdf default di komputer Anda.
2. Di baris Kasus "pdf", tolong ganti "pdf" ke ekstensi file yang ingin Anda cetak.

3. Silakan dan klik Tools > Referensi. Dalam bermunculan Referensi – Proyek1 kotak dialog, periksa Runtime Microsoft Scripting kotak, dan kemudian klik OK .

4. Simpan kode dan tekan lain + Q kunci untuk menutup Microsoft Visual Basic untuk Aplikasi jendela.

Catatan: Harap pastikan bahwa Aktifkan semua makro opsi diaktifkan di Outlook Anda. Anda dapat memeriksa opsi ini dengan mengikuti langkah-langkah yang ditunjukkan di bawah ini.

Langkah2: Buat aturan untuk menggunakan skrip

Setelah menambahkan skrip VBA di Outlook, Anda perlu membuat aturan untuk menggunakan skrip berdasarkan kondisi tertentu.

1. Buka tab Beranda, klik Peraturan > Kelola Aturan & Peringatan.

2. Dalam Aturan dan Peringatan kotak dialog, klik Aturan baru tombol untuk membuat aturan.

Tip: Jika Anda telah menambahkan beberapa akun email ke Outlook, tentukan akun di Terapkan perubahan ke folder ini daftar drop-down di mana Anda ingin menerapkan aturan. Jika tidak, itu akan diterapkan ke kotak masuk akun email yang saat ini dipilih.

3. Yang pertama Penyihir Aturan kotak dialog, pilih Terapkan aturan pada pesan yang saya terima dalam Langkah 1 kotak, lalu klik Next.

4. Di kedua Penyihir Aturan kotak dialog, Anda perlu:

4.1) Tentukan satu atau lebih kondisi dalam Langkah 1 kotak sesuai dengan kebutuhan Anda;
Dalam hal ini, saya hanya ingin mencetak lampiran dalam email masuk dari pengirim tertentu. Di sini, saya memeriksa dari orang atau kelompok publik kotak.
4.2) Klik nilai yang digarisbawahi di Langkah 2 kotak untuk mengedit kondisi;
4.3) Klik Next. Lihat tangkapan layar:

5. Di urutan ketiga Penyihir Aturan kotak dialog, Anda perlu mengkonfigurasi sebagai berikut.

5.1) Di Langkah 1: Pilih bagian tindakan, Periksalah menjalankan skrip kotak;
5.2) Di Langkah 2 bagian, klik teks yang digarisbawahi "skrip";
5.3) Dalam pembukaan Pilih Script kotak dialog, klik nama kode VBA yang Anda tambahkan di atas, lalu klik BAIK;
5.4) Klik Selanjutnya tombol. Lihat tangkapan layar:

Tip: Jika "menjalankan skrip” pilihan tidak ada di . Anda Penyihir Aturan, Anda dapat menampilkannya dengan mengikuti metode yang disebutkan dalam artikel ini: pulihkan pption Run A Script yang hilang di aturan Outlook.

6. Kemudian lainnya Penyihir Aturan muncul meminta pengecualian. Anda dapat memilih pengecualian jika perlu, jika tidak, klik tombol Selanjutnya tombol tanpa pilihan apa pun。

7. Terakhir Penyihir Aturan, Anda perlu menentukan nama untuk aturan tersebut, lalu klik tombol Finish .

8. Kemudian kembali ke Aturan dan Peringatan kotak dialog, Anda dapat melihat aturan yang Anda buat tercantum di dalamnya, klik OK tombol untuk menyelesaikan seluruh pengaturan.

Mulai sekarang, ketika email dari orang yang ditentukan diterima, file terlampir akan dicetak secara otomatis.


Terkait artikel

Hanya Cetak Lampiran (S) Dari Satu Email Atau Email Terpilih Di Outlook
Di Outlook, Anda dapat mencetak email, tetapi apakah Anda telah mencetak lampiran hanya dari satu email atau email tertentu di Outlook? Artikel ini memperkenalkan trik untuk menyelesaikan pekerjaan ini.

Hanya Cetak Header Pesan Dari Email Di Outlook
Saat mencetak email di Outlook, itu akan mencetak header pesan dan isi pesan di email. Namun, dalam beberapa kasus khusus, Anda mungkin hanya perlu mencetak header pesan dengan subjek, pengirim, penerima, dll. Artikel ini akan memperkenalkan dua solusi untuk melakukannya.

Cetak Kalender Dalam Rentang Tanggal Tertentu/Kustom Di Outlook
Biasanya, saat mencetak kalender dalam tampilan Bulan di Outlook, secara otomatis akan memilih bulan yang berisi tanggal yang dipilih saat ini. Namun, Anda mungkin perlu mencetak kalender dalam rentang tanggal khusus, seperti 3 bulan, setengah tahun, dll. Artikel ini akan memperkenalkan solusi untuk Anda.

Cetak Kontak Dengan Gambar Di Outlook
Biasanya, gambar kontak tidak akan dicetak saat mencetak kontak di Outlook. Tetapi terkadang, akan lebih mengesankan untuk mencetak kontak dengan gambarnya. Artikel ini akan memperkenalkan beberapa solusi untuk menyelesaikannya.

Cetak Pilihan Email Di Outlook
Jika Anda menerima pesan email dan menemukan bahwa ada pilihan konten email yang perlu dicetak alih-alih mencetak seluruh pesan, apa yang akan Anda lakukan? Sebenarnya, Outlook dapat membantu Anda mencapai operasi ini dengan bantuan browser internet, seperti Firefox dan Internet Explorer. Di sini saya akan mengambil browser Internet misalnya. Silahkan simak tutorialnya berikut ini.

Artikel lainnya tentang "mencetak di Outlook"...


Alat Produktivitas Kantor Terbaik

Kutools untuk Outlook - Lebih dari 100 Fitur Canggih untuk Meningkatkan Outlook Anda

🤖 Asisten Surat AI: Email profesional instan dengan keajaiban AI--satu klik untuk mendapatkan balasan jenius, nada sempurna, penguasaan multibahasa. Ubah email dengan mudah! ...

📧 Email Otomatis: Di Luar Kantor (Tersedia untuk POP dan IMAP)  /  Jadwal Kirim Email  /  Auto CC/BCC Sesuai Aturan Saat Mengirim Email  /  Penerusan Otomatis (Aturan Lanjutan)   /  Tambah Salam Otomatis   /  Secara Otomatis Membagi Email Multi-Penerima menjadi Pesan Individual ...

📨 email Management: Mengingat Email dengan Mudah  /  Blokir Email Penipuan berdasarkan Subjek dan Lainnya  /  Hapus Email Duplikat  /  Pencarian  /  Konsolidasi Folder ...

📁 Lampiran ProPenyimpanan Batch  /  Pelepasan Batch  /  Kompres Batch  /  Penyimpanan otomatis   /  Lepaskan Otomatis  /  Kompres Otomatis ...

🌟 Antarmuka Ajaib: 😊Lebih Banyak Emoji Cantik dan Keren   /  Tingkatkan Produktivitas Outlook Anda dengan Tampilan Tab  /  Minimalkan Outlook Daripada Menutup ...

👍 Keajaiban sekali klik: Balas Semua dengan Lampiran Masuk  /   Email Anti-Phishing  /  🕘Tampilkan Zona Waktu Pengirim ...

👩🏼‍🤝‍👩🏻 Kontak & Kalender: Batch Tambahkan Kontak Dari Email yang Dipilih  /  Bagi Grup Kontak menjadi Grup Individual  /  Hapus Pengingat Ulang Tahun ...

Lebih 100 Fitur Tunggu Eksplorasi Anda! Klik Di Sini untuk Menemukan Lebih Banyak.

 

 

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