• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Print attachments automatically and moves the mail to a new folder

Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#1
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,
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#2
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.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#3
Thank you for a quick and good answer! I will try this soon, and come back to you if I have any questions.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#4
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
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#5
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.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#6
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.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#7
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

 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#8
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.
 
Outlook version
Outlook 2016 64 bit
Email Account
Office 365 Exchange
#9
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.
 
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
#10
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
 

Similar threads