Solved

Save Email Attachments using VBA Excel

Posted on 2016-11-21
3
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

624 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