Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save Email Attachments using VBA Excel

Posted on 2016-11-21
3
Medium Priority
?
132 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 20

Accepted Solution

by:
darbid73 earned 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

926 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