Results 1 to 9 of 9
  1. #1
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283

    Importing Outlook Attachment into Access Table

    Hello,



    All I have been seeing is code for importing emails with the attachment linking into the table. I want to import the attachment from the email into the access table. Can anyone please supply some sample code or an example I would appreciate it. Thanks.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Don't have code for exporting attachment from Outlook email but I think that is what has to be done first. Save attachment out as external file, then pull the file into attachment field.

    Why embed into table? Embedding a lot of large files can quickly use up Access 2GB size limit.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    That's what I mean I don't want to embed it. I want to Save the file to a folder then import it. To automate it. But do you have code for importing emails from a specific folder in outlook without attachments?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    If by import you mean save into an Attachment field, that is what I meant by embed - into table field.

    I do not have code to import email without attachment but I have seen it 'out there'.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    For some reason it keeps showing that their are no items in my folders. when it gets to the line of code "Set objItems = cf.Items". I don't get it. And also, how do I assign the GetDefaultFolder to one particular folder?


    Public Sub ImportEmails()

    Dim rst As DAO.Recordset
    Dim db As DAO.Database
    Set db = CurrentDb
    Set rst = db.OpenRecordset("Producer_World")

    ' Set up Outlook objects.
    Dim ol As New Outlook.Application
    Dim olns As Outlook.NameSpace
    Dim cf As Outlook.MAPIFolder
    Dim cMail As Outlook.MailItem
    Dim objItems As Outlook.Items
    Dim of As Outlook.Folder
    Dim iNumMessages As Integer
    Set olns = ol.GetNamespace("MAPI")
    Set cf = olns.GetDefaultFolder(olFolderInbox)
    Set objItems = cf.Items
    iNumMessages = objItems.Count
    If iNumMessages <> 0 Then
    For i = 1 To iNumMessages
    If TypeName(objItems(i)) = "MailItem" Then
    Set cMail = objItems(i)
    rst.AddNew
    rst!Body = cMail.Body
    rst!ReceivedTime = cMail.ReceievedTime
    rst!SentOn = cMail.SentOn
    rst!Sender = cMail.Sender
    rst!Subject = cMail.Subject
    rst.Update
    End If
    Next i
    End If

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    I got this code to work http://www.blueclaw-db.com/read_emai...ss_outlook.htm
    Code:
    ' Set up Outlook objects.
    Dim ol As New Outlook.Application
    Dim of As Outlook.MAPIFolder
    Dim objItems As Outlook.Items
    Dim mo As Object
    Set of = ol.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
    Set objItems = of.Items
    For Each mo In objItems
    ...
    Next
    What do you mean by 'particular folder'? If you want a custom folder, I think have to use PickFolder.
    Set of = ol.GetNamespace("Mapi").PickFolder

    Everything I read about Outlook attachments says have to use SaveAsFile to export to folder and then code to save the file into Access Attachment field. Attachment field is a type of multi-value field. This requires 2 recordsets - a parent and child recordset.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    MTSPEER,
    Please give an example of what you are trying to do. You post is not clear.

    cMail.ReceievedTime is misspelled

    Do you want to save files attached to Outlook messages/emails to a specific folder, and record some info about the email and file(s) in Access. What exactly do you want to record about the file(s)? Sender, ReceivedDate,Subject....Filename, filesize..??

    I have done this in Outlook (years back) --any code I have is on a different machine but may be retrievable.

  8. #8
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283
    Thank you June7, that allowed me to select which folder I want to select from.

    Orange:Emails that I receive have .csv attachments to them, and I want to save these attachments to a folder out on my desktop then import the .csv files to a table that I have already created in access. And I just have those fields just to show who the email was from and when I got it.

  9. #9
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    The only way I could figure to reference a user-created inbox folder is if it is a subfolder under the default Inbox:
    Set of = ol.GetNamespace("Mapi").GetDefaultFolder(olFolderI nbox).Folders("Repairs")

    This is saving attachments to external location:
    Code:
        Dim atmt As Object
        For Each atmt In mo.Attachments
            atmt.SaveAsFile "C:\Users\Owner\June\Forums\" & atmt.FileName
        Next
    Here is guide for VBA to insert objects to attachment field https://msdn.microsoft.com/en-us/lib...ffice.12).aspx

    Alternatively, instead of embedding file into attachment field of table, just save the attachment file name into a text field. The full path can also be saved.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 12
    Last Post: 05-18-2017, 07:52 AM
  2. Replies: 3
    Last Post: 07-03-2016, 09:29 AM
  3. Add attachment to Outlook Email from Table?
    By floyd in forum Programming
    Replies: 3
    Last Post: 11-27-2013, 12:04 PM
  4. Replies: 5
    Last Post: 10-08-2013, 08:16 AM
  5. Importing Outlook 2010 folders into Access
    By the_chomp in forum Access
    Replies: 3
    Last Post: 01-22-2013, 06:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums