Solved

Save Email Attachments using VBA Excel

Posted on 2016-11-21
3
47 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

25 Experts available now in Live!

Get 1:1 Help Now