Solved

how to extract files from outlook 2013 with vba on access

Posted on 2014-12-08
3
271 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
[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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

739 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