Solved

how to extract files from outlook 2013 with vba on access

Posted on 2014-12-08
3
267 Views
Last Modified: 2014-12-17
hi


i have this code to loop on microsoft access 2013 form that loop all emails on outlook. what i have to add to the  code to check every email if it's have any attachment file and if so to extract it.

this is the code
Dim oApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oMailItem As Outlook.MailItem

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)

For Each oMailItem In oInbox.Items
    MsgBox oMailItem.Attachments
Next

Open in new window

0
Comment
Question by:bill201
3 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40486455
You would do a look on oMailItem.Attachments() and, use .SaveAsFile(strDirectoryAndFileName)
0
 
LVL 26

Accepted Solution

by:
Nick67 earned 500 total points
ID: 40487204
Given that Outlook only wants one instance open, fire it up with the following code.
The rest should get you very close to what you need

Option Explicit
Option Compare Database

Public Function FireOutlook() As Outlook.Application
On Error Resume Next
Dim objOutlook As Outlook.Application

Set objOutlook = GetObject(, "Outlook.Application")
'MsgBox Err.Number & " " & Err.Description
If Err.Number = 429 Then
    Err.Clear
    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")
End If

Set FireOutlook = objOutlook

End Function


Sub GetAttachments()
Dim OutlookApp As Outlook.Application
Dim oNameSpace    As Namespace
Dim oFldrList     As Outlook.MAPIFolder

Dim ThePath As String
Dim ns As Namespace
Dim Item As Object
Dim olItem As MailItem
Dim olAtt As Outlook.Attachment
Set OutlookApp = FireOutlook
Dim i As Integer
i = 0
ThePath = "c:\temp\" 'change this to suit you
Set oNameSpace = OutlookApp.GetNamespace("MAPI")
Set oFldrList = oNameSpace.GetDefaultFolder(olFolderInbox)

For Each olItem In oFldrList.Items
    For Each olAtt In olItem.Attachments
       olAtt.SaveAsFile ThePath& olAtt.FileName
       i = i + 1
    Next
Next

If i > 0 Then
   MsgBox "I found " & i & " attached files." _
      & vbCrLf & "I have saved them on the" & ThePath & " Path." _
      & vbCrLf & vbCrLf & " ", vbInformation, "Download Finished!"
Else
   MsgBox "I didn't find any attached files in your mail.", vbInformation, _
   "Finished!"
End If

End Sub

Open in new window

0
 

Author Closing Comment

by:bill201
ID: 40505904
thanks alot
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

830 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