Print attachments automatically and moves the mail to a new folder

Status
Not open for further replies.

Di2sel

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
Hello,

First of all I want to say that I have done some research on the forum, and can't seem to find any solution to my problem. As my Outlook is of newer version and that it is 64-bit instead of 32-bit.

This is my challenge:
I receive daily a lot of emails with attachments that I want to print out. The attachments are both in .xlsx and .pdf format.
I tried to make a rules that said print out every email with "invoice" in the subject or body. However, that printed only the body of the email, and not the attachments, which is what I want.
I am currently using office proffesional plus 2016. 64-bits version.

Can anyone help me with this? I'm trying to learn VBA. But I currently don't have a lot of time to practice, so if anyone know where I can find such a script. Or help me out, I would be forever grateful.
Macro to Print Outlook email attachments as they arrive
Print Attachments Automatically - VBOffice

I found these links. But they are for 32-bit, and older Outlook versions.
I don't want to try the script before I am entirely sure its correct for my environmeny. In case it suddenly prints out all the attachments in all folder.

Question 1: How would a script like this look like. Any code examples?
Question 2: If I activate a script, is it enough if I choose the folder it will operate in, within the rules menu? Or does the folder have to be hard coded? I want this script to only operate within a certain shared subfolder.
-----------------
The process looks like this:
1. Find all the attachments in a chosen mailbox, with some chosen keywords in the body/subject field.
2. Print all these attachments.
3. Move the mail to a different mailbox after it has printed the attachment. (Can this be done in the same rule? Just add "move" after "run script")

Thanks,
 
I don't want to try the script before I am entirely sure its correct for my environmeny.
Most macros work with either 32 or 64bit. Those that don't will error & fail on 64bit until you make a minor change. The macros at slipstick has instructions on using them with 64bit.

The first one at slipstick and at vboffice is an itemadd macros that run on new items added to a folder. You can test them by either copy and pasting a message in place (to create a duplicate) or setting it to watch a different folder and moving or copying a message to that folder. It won't print out all as written.

Question 1: How would a script like this look like. Any code examples?
Question 2: If I activate a script, is it enough if I choose the folder it will operate in, within the rules menu? Or does the folder have to be hard coded? I want this script to only operate within a certain shared subfolder.

The version at Macro to Print Outlook email attachments as they arrive will run on selected messages. it could be changed to run on all messages.
 
Thank you for a quick and good answer! I will try this soon, and come back to you if I have any questions.
 
I am now working on implementing the script.

Set Ns = Application.GetNamespace("MAPI")
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items

Can you explain to me what this part of the code does= it sets the default folder to "olFolderInbox", but which folder is this. Is this the current folder in focus? I'm asking because I want my script to run in a different folder than the inbox. It should run in a shared subfolder.

Would you also advice me to combine the script with rules. Example:
Rule: Find all mail messages with invoice in the subject
Rule: Run script
Rule : Move folder
 
Set Folder = Ns.GetDefaultFolder(olFolderInbox)
Set Items = Folder.Items
This line sets the inbox as the folder and the items in it as 'items'.

if you want to run it on the selected folder, use currentfolder:
Set Folder = Ns.ActiveExplorer.CurrentFolder

Working with VBA and non-default Outlook Folders has more information on how to call different folders. If its always going to run on all items in a specific folder, you can set it in the code and run the code from anywhere in outlook. if you want to run it on any folder manually, use current folder.
 
Would you also advice me to combine the script with rules. Example:
Rule: Find all mail messages with invoice in the subject
Rule: Run script
Rule : Move folder
rules will only run on the inbox and only on incoming messages. if you want to use it on a folder in a shared mailbox, you could use an itemadd macro - watch the folder for items added to the folder.
 
Thank you for all the help so far, Diane! I really appreaciate that you take your time to answer. After looking at your answers, and done some modifications on my own, I have come up with this code. I have also seen your video on how to run a script in outlook.

Now I get the error message. "The macro in this project are disabled". I have turned off the macro security settings, and restarted the application.

I can't seem to run the code. It prompts me with a menu to open "Module1" and insert the code there. But as the guide says, I have to paste the code into MyOutlooksession. Any ideas?

The email I inserted in this example is not the one I use in my project.

Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias _ "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, _ ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private WithEvents Items As Outlook.Items Private Sub Application_Startup() Dim Ns As Outlook.NameSpace Dim Folder As Outlook.MAPIFolder Set Ns = Application.GetNamespace("MAPI") Set Folder = Ns.ActiveExplorer.CurrentFolder Set Items = Folder.Items End Sub Private Sub Items_ItemAdd(ByVal Item As Object) If TypeOf Item Is Outlook.MailItem Then PrintAttachments Item MovePrintedMail Item End If End Sub Private Sub PrintAttachments(oMail As Outlook.MailItem) On Error Resume Next Dim colAtts As Outlook.Attachments Dim oAtt As Outlook.Attachment Dim sFile As String Dim sDirectory As String Dim sFileType As String sDirectory = "W:\Desktop\MailFolder" Set colAtts = oMail.Attachments If colAtts.Count Then For Each oAtt In colAtts ' This code looks at the last 4 characters in a filename sFileType = LCase$(Right$(oAtt.FileName, 4)) Select Case sFileType ' Add additional file types below Case ".xls", ".pdf", ".xlsx" sFile = sDirectory & oAtt.FileName oAtt.SaveAsFile sFile ShellExecute 0, "print", sFile, vbNullString, vbNullString, 0 End Select Next End If End Sub Sub MovePrintedMail(oMail As Outlook.MailItem) Dim objDestFolder As Outlook.MAPIFolder Set objDestFolder = Session.Folders("myemail@email.com").Folders("Innboks").Folders("Ferdig") oMail.Move objDestFolder Set objDestFolder = Nothing End Sub
 
it definitely needs to be in thisoutlooksession. When you first open the VBA editor, it will often open to whatever module was opened last, so ignore that and find thisutlooksession.

Changing the security settings and restarting should have fixed the error - choose no security (run all macros) while testing - you can use selfcert to sign it and change the security settings after you are sure it works.
 
Thanks, that helped. I don't get the error message anymore. It was as you said, it opened module1 because it had been opened from before.

I've tried running the code from VBA editor and from the developer tab, but I don't think the code is correct. Nothing happens. Since it's an Itemadd macro I tried to send a mail, but it didn't react to the mail. Nothing really happens whenever I try to launch it.

Must be something wrong with my code.
 
I've tried running the code from VBA editor and from the developer tab, but I don't think the code is correct. Nothing happens. Since it's an Itemadd macro I tried to send a mail, but it didn't react to the mail. Nothing really happens whenever I try to launch it.
It's an auto macro - it triggers when outlook first starts - or you can kick start it by clicking n the application_startup macro and clicking run. It then watches the folder set by the app startup macro for new items - you can copy and paste in place (select message then ctrl+c, p) or drag a message to the folder to trigger it.

Oh... this won't work in an autostart - Set Folder = Ns.ActiveExplorer.CurrentFolder. if you are going to run it manually, you don't need the autorun.

if you want to run it using a rule, rename
Private Sub Items_ItemAdd(ByVal Item As Object)

to
Private Sub PrintMove(Item As mailitem)
then set up a run a script rule.

if you want to run it manually on all items in the folder or on the selected item(s), you would use one of the macros at Working with All Items in a Folder or Selected Items and replace
' do whatever
Debug.Print .Subject
with

If TypeOf Item Is Outlook.MailItem Then
PrintAttachments Item
MovePrintedMail Item
End If
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
I Print Automatically Attachments Outlook VBA and Custom Forms 3
C automatically print attachments Using Outlook 4
5 Automatically print email attachments Using Outlook 7
G Print email attachments when hit subfolder Outlook VBA and Custom Forms 1
M Print email and, attachments sent in hyperlinks in the email Outlook VBA and Custom Forms 2
D Print Attachments only in selected emails using a macro Outlook VBA and Custom Forms 3
J Auto print PDF attachments as they arrive with certain words in subject Outlook VBA and Custom Forms 3
M Print Attachments from another mailbox (not default) Outlook VBA and Custom Forms 2
O For Outlook 2007 - VBA to print attachments Using Outlook 1
J Outlook 2010 using Quick Print to print attachments Using Outlook 1
A Outlook 11 for Mac won't print e-mail attachments Using Outlook 6
P Possible to write a macro to print all attachments with specific . Outlook VBA and Custom Forms 1
Z Re: Auto Print email and word attachments on send Outlook VBA and Custom Forms 1
U Outlook not responding when trying to print Emails Using Outlook 6
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
S Cannot print Contacts Using Outlook 7
G print attachment straight away; working script edit not working Outlook VBA and Custom Forms 0
S CONTACT FIELD PRINT ORDER Outlook VBA and Custom Forms 1
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
B Outlook 2016 Outlook crashes when trying to print certain emails Using Outlook 5
M Batch print without appended trail of repeated e Using Outlook 2
Witzker print-list-of-outlook-folders with sort posibility Outlook VBA and Custom Forms 7
M Custom Calendar Print Suggestions? Using Outlook 0
A Day view - print appointment details Using Outlook 1
I print calendar without subject and details Using Outlook 1
oliv- property "is printed" or catching print events Outlook VBA and Custom Forms 2
Diane Poremsky Print a list of your Outlook folders Using Outlook 0
Diane Poremsky Combine and Print Multiple Outlook Calendars Using Outlook 0
B Print list of previously sent recipients Using Outlook 1
R Print email message and attachment in order Outlook VBA and Custom Forms 2
L Outlook 2002: HTML Emails Will Not Print: Please Help Using Outlook 0
A Print first page of a new email Outlook VBA and Custom Forms 7
Diane Poremsky Print Monthly or Work Week Calendars Using Outlook 0
Diane Poremsky No drop down calendars in Outlook 2010 Print Options Using Outlook 0
Diane Poremsky Print Monthly or Work Week Calendars Using Outlook 0
S Macro to print & move selected emails? Using Outlook 3
H Problems With Outlook 2013 VBA To Send and Print an email Outlook VBA and Custom Forms 1
D Outlook 2013 Categories won't print In color Using Outlook 2
G Calendar monthly view - Print just 3 weeks Using Outlook 5
R Can't modify Outlook view font with IE anymore (even though IE still affects print font) Using Outlook 5
E Outlook VBA to print attached Pdf to a fax printer and assign fax number Using Outlook 0
L Outlook 2010 Quick Print Attachment, nothing happend Using Outlook 0
J Automatically Print PDF When They Are Received Using Outlook 4
M button to send and print emails Using Outlook 26
S How to print ONLY first line of appointments in month view? Using Outlook 1
M print free/busy schedule of an user Using Outlook 2
W Cannot print Outlook 2007 emails Using Outlook 2
R problem with incomming e-mail I am unable to print full e-mail letter Using Outlook 1
M Custom print contacts Using Outlook 2

Similar threads

Back
Top