Auto Save an attachment??

  • Thread starter Thread starter Keith
  • Start date Start date
K

Keith

I receive several emails each day with, most with a different subject line,
(but the same each day), some vary slightly each day with the suffix (eg
report2.csv, report3.csv) and all with attachments (mainly txt files), I
have to save those attachments into various folders to then import into
access. As a complete novice to Outlook VBA, is it possible to automate this
so that as soon as these are received, the attachments are saved to the
relevant folders? where do I enter the code, what do I need to do?

This is on corporate email through a microsoft exchange server

I have a reasonable knowledge of VBA for Excel if that helps?

Thanks
 
Here are your best options:

1) Create a rule that fires a custom script when the incoming message
conditions are met (this rule is a special VBA procedure). See "How to
create a script for the Rules Wizard in Outlook" at
http://support.microsoft.com/d­efault.aspx?scid=KB;en-us;q306­108 for more
info. However, as we all know - rules sometimes don't always work well. But
if you can depend on the rule, your VBA script that is fired will then do
whatever you want to the message.

2) Write VBA code or a COM Add-In that traps the ItemAdd event of the
Inbox's Items collection. However, the Item_Add event that is used to
process incoming messages is flaky and may not fire if many new messages are
downloaded at the same time. Although if processed messages are moved to
another folder, ones that inadvertently get missed can be processed by
clicking a custom toolbar button to run a macro manually.

This above approach breaks your dependency on rules, but you'd have to add
the logic to determine if the e-mail message is the one you're looking for.

3) A better approach (if you are using Exchange): create an Exchange Event
Sink with the logic inside a COM+ .dll developed using Visual Basic 6/.NET or
Visual C++ that will run on your mail server. This is guaranteed to fire and
process ALL incoming messages.

Regardless, once you have a MailItem object, use the MailItem.Attachments
collection to get individual Attachment objects and use the SaveAsFile method
to output it to the file system.

For some great starting resources for programming with Outlook, see this link:

Developer Learning Center for Microsoft Outlook:
http://www.outlookcode.com/d/index.htm

For more info on automatic message processing, see these links:

Description of programming with Outlook rules:
http://support.microsoft.com/?kbid=324568

Beyond the Rules Wizard:
http://msdn.microsoft.com/library/en-us/dnovba01/html/RulesWizard.asp?frame=true

How to create a custom rule using Visual Basic for Applications (VBA) in
Outlook:
http://support.microsoft.com/default.aspx?scid=kb;en-us;292063
 
Thanks for this,

However, I have followed the procedure in the link below, but it still
doesn't work? This is the code I put into the 'ThisOutlookSession' module,
any ideas?

Dim WithEvents objInbox As Outlook.Items

Private Sub Application_Startup()
Set objInbox = Session.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub objInbox_ItemAdd(ByVal Item As Object)
If Item.Class = olMail And Item.Subject = "Test" Then
If Item.Attachments.Count > 0 Then
Dim objAttachments As Outlook.Attachments
Set objAttachments = Item.Attachments
For Each objAttach In objAttachments
' Does not handle duplicate filename scenarios
objAttach.SaveAsFile "C:\Test\" & objAttach.FileName
Next
Set objAttachments = Nothing
End If
End If
End Sub
 
The code looks fine to me. Remember though - the ItemAdd event is flaky and
is not guaranteed to fire for every new item, especially when a bunch are
created or downloaded at once.

Also, if you are using Outlook 2003 with Exchange, you can now use the
NewMailEx, which AFAIK is 100% reliable (in theory).

One more thing - if you haven't restarted Outlook, that could be why it is
not firing. You can simulate this by putting your cursor inside the
Application_Startup procedure and running it (F5). You could also create the
Application_NewMail event stub with no code and put a breakpoint there to
verify that everything is ready to go.
 
Back
Top