How to Access a VBA Email Attachment
Having the knowledge on how to save email attachments using Visual Basic for Applications (VBA) can save you time when working with attachments. You can use VBA in any of the Microsoft Office applications to automate processes such as saving email attachments from Outlook. In a few steps, you can write VBA code to save email attachments in any particular folder in your computer.
Instructions
-
-
1
Start Microsoft Access 2007, select "Database Tools" and click on "Visual Basic." Click the "Tools" menu and select "References." Check the box next to "Microsoft Outlook 12.0 Object Library" and select "OK."
-
2
Type the following to create a sub:
Sub getOutlookAttachments ()
Press "Enter"
-
-
3
Type the following to create your variables:
Dim nameSpace As nameSpace
Dim InboxFolder As MAPIFolder
Dim outlookItem As Object
Dim mailAttachment As Outlook.Attachment
Dim fName As String
-
4
Type the following to define your variables:
Set nameSpace = GetNamespace("MAPI")
Set InboxFolder = nameSpace.GetDefaultFolder(olFolderInboxFolder)
-
5
Type the following to loop through your inbox folder, search for attachments and save them to C:\:
For Each outlookItem In InboxFolder.outlookItems
For Each mailAttachment In outlookItem.Attachments
fName = "C:\" & mailAttachment.fName
mailAttachment.SaveAsFile fName
Next mailAttachment
Next outlookItem
-
6
Type the following to release your variables from memory:
Set mailAttachment = Nothing
Set outlookItem = Nothing
Set nameSpace = Nothing
Execute your sub to save your Outlook email attachments to "C:\"
-
1
References
- Photo Credit email image by Soja Andrzej from Fotolia.com