Solved

Save Email Attachments using VBA Excel

Posted on 2016-11-21
3
61 Views
Last Modified: 2016-11-22
Hello...I'm a complete novice in the VBA world and attempting to learn as much as possible from experts like yourselves and need all the help you can assist me with.

My objective is to extract all email attachments in a predefined email account and save them to a designated network folder.  After detaching the email attachments, I am supposed to move the emails to specified folders within the email account.  The VBA I am using was adapted from Ron Bruin.  I am using MS Office 2010 and Windows 7.

Problems I am experiencing with the code:
1.  Code stops processing even though has not finished looping thru all the emails in the email account.
2.  Some of the attachments it extracts can not be read.  When I try to open the attachment (usually an Excel file), I get the following error message:  "The file you are trying to open is in a different format than specified by the file extension.  Verify that the file is not corrupted and is from a trusted source before opening the file.  Do you want to open the file now?"  When I do, the file contains garbage.  The thing is that when I go to the actual email containing the attachment, and open the attachment from the email, the file is fine and does not prompt me with the error message indicated above.  I haven't been able to figure out why it is doing this.  

File attachments from the emails come in different formats.  Some are Word, Adobe and even Zipped folders, but most are Excel.  Any advise you can provide, is of great assistance.  Thank you!
Email-Attachment-Extraction.xlsm
0
Comment
Question by:Yiyi0912
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
darbid73 earned 500 total points
ID: 41897029
Hi,

It appears that you have a Collection of Email items from a folder. You are moving the items from this folder within the loop.  This means that you are altering the collection while you are looping.

For a loop where you are removing / deleting or moving items within the loop you need to use a loop like this where it counts backwards.  You will have to therefore SET your email each time.

For i = (strInboxFolder.Items.Count - 1) To 0 Step -1
    Set E_Mail = strInboxFolder.Items(i)
    
Next i

Open in new window

0
 
LVL 1

Author Comment

by:Yiyi0912
ID: 41897521
Thank you so very much!  I've altered the code as you recommended and it seems to be working perfectly!  Many thanks for your time.
0
 
LVL 1

Author Closing Comment

by:Yiyi0912
ID: 41897823
Thank you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now