using script rule to save attachments on arrival Outlook 2010

Status
Not open for further replies.
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Thanks for the wonderful educational post on saving attachments to the hard drive, Diane! I tried to post this question under that stream, but kept getting errors, so decided to try this method.
I am new to VBA with Outlook.
I am trying to make a rule that runs on message arrival to save all attachments to a folder.
I pasted in the code for “run a script rule to save attachments” as specified into the VBA editor and
I created the following rule called save attachments on arrival:
Apply this rule when the message arrives
which has an attachment
and on this computer only
run Project1.ThisOutlookSession.SaveAttachments
Unfortunately, the rule does NOT run on arrival of the message. It DOES work perfectly when the rule is run manually.

I am trying to solve a problem for a small client who has been using Eudora for years and Eudora automatically saves every attachment to an Attachment directory on arrival. This VBA rule script seems like it could be the simplest solution to make Outlook feel more like Eudora, if I can reliably make it run on arrival. Also I would need to be able to set up this rule with this script on multiple computers that currently do not have VB installed. Is this even possible?

I am still testing the Eudora environment to understand how it handles attachments of the same name and signatures, but I have not completed that assessment yet.

I am using VBA 2015 Community in a Windows 7 environment running Outlook 2010.

Thanks for your help!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Also I would need to be able to set up this rule with this script on multiple computers that currently do not have VB installed. Is this even possible?
You need VBA addin enabled in Outlook - nothing more - so yes, it will work.

Try adding a module to outlook (right click on Project1 in the editor > New > Module) and putting the macro in it - run a script rules usually work better if the script isn't in ThisOutlookSession (at least for me). Or, forget the script and use an itemadd macro. I have an itemadd macro at Save Attachments to the Hard Drive
 
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Thanks, Diane! I liked the sound of the item add so I just copied the itemadd from your link [this macro runs (automatically) on messages as they are added to the Inbox. Put it in ThisOutlookSession.] into ThisOutlookSession and saved it. I created a folder for OLAttachements under My Documents (default location). I then sent a test email with attachments into the inbox, but it did not appear to run automatically when the email hit the inbox. Do I need to do something other than save it? This time it specifically advises saving in the ThisOutlookSession... should it be somewhere else?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
You need this one in thisoutlooksession - macro security needs to be set to low and then you need to restart outlook to trigger the startup macro that sets the folder to watch. If macro security was already set to low, you can click in the application_startup macro and click Run to trigger it without restarting.
 
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
I restarted Outlook without any change, then I changed the outlook macro security as follows:
Outlook 2010 Options > Trust Center > Trust Center Settings > Macro Settings to be "enable all macros". I am still not seeing the attachments in the "My Documents>OLAttachments" folder.
Where do I find the application_startup macro to click Run?
(I am concerned about the implications about enable all macros for security)
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This is the first part of the itemadd macro at the link i posted earlier (second macro on page) - the complete macro goes in ThisOutlookSession. Macro security should be set to low during testing - when it works, you can sign it using selfcert and change the security to allow signed macros.

When you restart outlook the startup macro should run, or click in the Application_Startup then click Run to kick start it during testing.

Code:
Private WithEvents olInboxItems As Items

Private Sub Application_Startup()
  Dim objNS As NameSpace
  Set objNS = Application.Session
  ' instantiate objects declared WithEvents
  Set olInboxItems = objNS.GetDefaultFolder(olFolderInbox).Items
  Set objNS = Nothing
End Sub


Private Sub olInboxItems_ItemAdd(ByVal Item As Object)
  On Error Resume Next
If Item.Attachments.Count > 0 Then

***snip***
 
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Hi Diane,
I am just getting back to trying this again and I am still not seeing attachments saved into the OLAttachments folder that I created under My Documents.
I have my macro security set to execute all macros.
So for troubleshooting purposes, I intentionally introduced an error into the code just to see try to verify it was being executed.
This compile error happens immediately upon logging into Outlook, so this seems to prove that it is trying to execute.
(Invalid attribute in sub or Function) (my screen shot disappeared)

That seems to infer that the code is being executed, but for some reason is not saving the attachments.
Any suggestions on what else to check?
I am doing this with Outlook set up as IMAP for a FASTMAIL account this time.
I have not tried POP3 yet… does that make a difference on how this would work?
Thank you for your help!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
IMAP can be a bit goofy as the full message isn't downloaded immediately, like it is with pop3. In Send & Receive settings (Ctrl+Alt+S, then Edit, select the imap account), are you downloading full messages?
 
Outlook version
Outlook 2010 32 bit
Email Account
IMAP
Yes, I think I am set up to download the full message and I verified the inbox is a subscribed folder.
I was unable to attach a screen shot, but I have "download complete items including attachments for subscribed folders" selected under Receive mail items.

I noticed that this ItemAdd VBA code is set up for Application Startup.
I see there is an option in the dropdown on the right for NewMail instead of Startup. Would that make a difference?
When testing, I am always closing and re-starting Outlook, so Startup should be working, but it is still not saving the attachments to OLAttachments folder.

We are looking into setting up a POP3 account for testing, but have not had a chance to do that yet.
Your help is MUCH appreciated!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
I noticed that this ItemAdd VBA code is set up for Application Startup.
The code in app startup (usually just sets a folder to watch, but could do more) is run when outlook starts. If it works once then dies, something is causing the macro to fail and forget the variable that was set. It could be a code failure (if you click End when the debugger comes up) or the variable is set to none in the code - in this case, setting olInboxItems = nothing in the macro that uses it would prevent it from working again.

I see there is an option in the dropdown on the right for NewMail instead of Startup. Would that make a difference?
I'm not sure where you are looking, but no, it probably won't make a difference.

Add msgbox "Macro running App Start" before the end sub in the app start macro and msgbox "Macro running - itemadd" at the beginning in the other macro and restart. Does either dialog box come up? If so, something else is failing.
 
Status
Not open for further replies.
Top