Link to home
Start Free TrialLog in
Avatar of V B
V BFlag for United Arab Emirates

asked on

Outlook VBA : Loop through an outlook folder and Save attachments with file name as avalue from message body

Hi,

I have an outlook folder where i keep unread messages from a sender, these mails have attachments which i would like to save to my desktop folder. But while saving i would like the file name to be a string from the message body.

For e.g.. the message body contains a string "ABC Corp", i would like to save the attachment using this string value as file name. each message body would have a different string.

Can someone guide me in the right direction to execute this using vba in outlook ?

Thanks
VB
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Since the string is variable how is the computer supposed to know which string to use as the filename. Computers are the dumbest things on earth, they only do things quickly and will repeat an instruction until told to quit. Unlike animate objects that quickly tire. Humans unlike computers can try and guess .. computers only follow strict rules
Avatar of V B

ASKER

Hi David,

thank you for taking time to respond to my question, the string i am looking for is available in the first few lines of the mail body and is standard across all emails, a sample is given below

Please find attached your xxxxxxx eBill for,

Account Number: 0123456789
Bill Period: 01/06/15 to 30/06/15
Due Date: 15/07/15

i want to save the attachment using the account number for each mail. I was wondering if a regular expression would do the trick, still trying it out, thought someone in EE might have already found a way to do it.



VB
ASKER CERTIFIED SOLUTION
Avatar of omgang
omgang
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of V B

ASKER

Thank you Omgang,

i will test this out and let you know. Once again thank you for your effort.

vineeth
Avatar of V B

ASKER

Hi Omgang
i have tested this with one mail in a folder and looks like its working ok for one mail, and i believe this should work fine with multiple mails in a folder as well, i am checking this in the next few days.

will update the results here, thanks for your outstanding solution

VB
Avatar of V B

ASKER

Hi Omgang,

sorry for the delay in responding back to you. I was actually testing this on a large scale project, and i have hit a road block. when i try to run this on a folder which has multiple emails, i get the error message

" -2147221233 (The attempted operation failed.  An object could not be found.) in procedure GetAcctNumFromMsg of Module Module4 "

I did a debug and looks like its failing at the "olFolder", as in, it cant find the folder, although i have defined the folder under the inbox, and i can see the folder structure within the inbox.

I am trying to do some research of my own on this, but if you could have a look at this when you get some time, that would be great.

VB
Avatar of V B

ASKER

Hi Omgang,

i think i got it working, a bit of tweak to your code, i will test this with our test unit and post the results.

VB
Sorry I missed your posts over the weekend.  Let me know if you need anything.
If the folder where the messages are stored is a subfolder of another folder the code will need to be modified a bit.  Perhaps a folder picker instead of hard coding the folder name?

OM Gang
Avatar of V B

ASKER

Hi OMGang,

I should get the results out tomorrow, will update it over here. The crux of the application is the ParseString function that you wrote, this works like magic.

VB
Avatar of V B

ASKER

Hi OMGang,

i would like to thank you for this amazing solution that you have provided, this works like magic :)

I am marking this as the accepted solution.

Once again, thank you for your efforts.

VB
Avatar of V B

ASKER

OMGang was fast in responding and coming up with a solution that required no change and worked like magic.