Results 1 to 11 of 11
  1. #1
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17

    Save Outlook Attachments in a Dir

    Hi again,

    I expandet my code as follow (save the attached files from Outlook - after: If Not Err.Number <> 0 Then):

    Code:
    Option Compare
    DatabaseOption Explicit
    
    
    Private Sub Befehl17_Click()
    ' ##################### OutlookImport ##################################
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim outObject, Mapi, Inbox, InboxImported
    Dim i As Integer
    Dim strSQL As String
    
    
    Set rs = CurrentDb.OpenRecordset("OutlookImport")
    Set outObject = CreateObject("Outlook.Application")
    Set Mapi = outObject.GetNamespace("MAPI")
    Set Inbox = Mapi.GetDefaultFolder(olFolderInbox).Folders("import")
    Set InboxImported = Mapi.GetDefaultFolder(olFolderInbox).Folders("imported")
    
    
        For Each Mail In Inbox.Items
        
        strSQL = "INSERT INTO OutlookImport (AbsenderMail, AbsenderName, SendTo, SendCC, Betreff, MailDatum, Nachricht, EntryID) VALUES ('" & Mail.SenderEmailAddress & "', '" & Mail.SenderName & "', '" & Mail.To & "', '" & Mail.CC & "', '" & Mail.Subject & "', '" & Mail.SentOn & "', '" & Mail.Body & "', '" & Mail.EntryID & "');"
    
    
            DoCmd.SetWarnings False
            On Error Resume Next
            DoCmd.RunSQL strSQL
            DoCmd.SetWarnings True
        
             If Not Err.Number <> 0 Then
                If Mail.Attachments.Count > 0 Then                
                    AnzAttach = Mail.Attachments.Count
                    strPath = "C:\Unterlagen\" & Replace(Mail.SenderName, " ", "") & "_" & Replace(Replace(Mail.SentOn, " ", "_"), ":", ".")
                    Debug.Print strPath
                    MkDir strPath
                    
                    For i = 1 To AnzAttach
                        Mail.Attachments.Item(i).SaveAs strPath & Mail.Attachments.Item(i).FileName
                    Next i
    
    
                    Mail.Move InboxInBearbeitung 'Mails verschieben in Bearbeitung
                End If                    
             End If
        Next
    
    
        Forms![OutlookImport].Requery
    End Sub
    the directroy "Unterlagen" allready exist.

    So, the definition of the path works (strPath)
    the creation of the path wirks (MkDir)

    but the SaveAs command did not work, the new directories are empty.

    I think, the syntax is correctly, isīnt it?

    any ideas?

    thx

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Appear to be missing \ character in the path concatenation to file name.

    Mail.Attachments.Item(i).SaveAs strPath & "\" & Mail.Attachments.Item(i).FileName

    I found example code that used SaveAsFile instead of just SaveAs.
    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
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    Hi June7,

    thx for reply but nope, ist want wokr :/

    SaveAsFile was my first try but with that I get a filemane including the directory name like c:\Unterlagen\blabla.pdf

    so, thats not what I want

    Edit:

    sorry, it works with your &"\" and!!! SaveAsFile.



    once again: thx for quick help

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    I got this to work.
    Code:
    Dim olApp As Outlook.Application, olMail As Outlook.MailItem, olAtt As Outlook.Attachments, olSel As Outlook.Selection
    Dim i As Integer
    Set olApp = CreateObject("Outlook.Application")
    Set olSel = olApp.ActiveExplorer.Selection
    On Error Resume Next
    For Each olMail In olSel
        Set olAtt = olMail.Attachments
        If olMail.Attachments.Count > 0 Then
            For i = olMail.Attachments.Count To 1 Step -1
                olAtt.Item(i).SaveAsFile "C:\Temp\" & olAtt.Item(i).FileName
            Next i
        End If
    Next
    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
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    Okay, now it works and I try to save the folder in my table to go to from a form if I want. I thought it must be simple and try this code:
    Code:
    Mail.Attachments.Item(i).SaveAsFile strPath & "\" & Mail.Attachments.Item(i).FileName
    strSQLAtt = "INSERT INTO OutlookImport (AttachFolder) VALUE ('" & strPath & "');"
    DoCmd.RunSQL strSQLAtt
    I was sure, that it works so simple as it looks like, but I was wrong.

    So what is my misstake?

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    What do you mean by 'save the folder in my table'? Just the path string into a text field?
    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
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    I mean to save only the string "c:\Unterlagen\..."

    I will use the string to open the folder in the windows explorer by pressing a button in a form.

    and I create a new field "AttachFolder" as you can see. It is impossible to get the Me.AttachFolder. No Auto complete, and if I try to compile the code, I get "method or data object not found"

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Should be VALUES

    However, I would use:

    CurrentDb.Execute strSQLAtt
    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.

  9. #9
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    why it is better to use
    "CurrentDb.Execute strSQLAtt"
    instead of
    "
    DoCmd.RunSQL strSQLAtt"

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Execute does not trigger warning messages that RunSQL does so don't have to bother with SetWarnings.

    There may be other benefits that have to do with other arguments of Execute method.
    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.

  11. #11
    NiMora is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2015
    Location
    Germany
    Posts
    17
    okay, that: Should be VALUES

    solve the problem.

    sorry for my blindnes

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

Similar Threads

  1. Importing Outlook attachments
    By DHIGHTO in forum Import/Export Data
    Replies: 3
    Last Post: 02-10-2015, 12:58 PM
  2. Replies: 15
    Last Post: 01-28-2014, 12:20 PM
  3. getting outlook attachments using vba code
    By umenash in forum Access
    Replies: 3
    Last Post: 10-02-2013, 12:15 PM
  4. Save Attachments.
    By drunkinmunki in forum Programming
    Replies: 6
    Last Post: 11-20-2012, 10:11 AM
  5. Replies: 2
    Last Post: 10-24-2012, 02:09 AM

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