Home > Enterprise >  Save all outlook attachments
Save all outlook attachments

Time:08-11

I want to save all attachments from my Outlook 365 inbox.

Copying this tutorial I wrote:

Sub Download_Attachments()

    Dim ns As NameSpace
    Dim olFolder_Inbox As Folder
    Dim olMail As MailItem
    Dim olAttachment As Attachment

    Dim fso As Object
    Dim File_Saved_Folder As String

    File_Saved_Folder_Path = "C:\GIS\temp\mails"

    Set ns = GetNamespace("MAPI")
    
    Set olFolder_Inbox = ns.GetDefaultFolder(olFolderInbox)
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    For Each olMail In olFolder_Inbox.Items
    
        If TypeName(olMail) = "MailItem" And olMail.Attachments.Count > 0 Then
        
            fso.CreateFolder (fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)))
            
            For Each olAttachment In olMail.Attachments
            
                olAttachment.SaveAsFile fso.BuildPath(File_Saved_Folder_Path, Trim(olMail.Subject)) & "\" & olAttachment.FileName
                                    
            Next olAttachment
        
        End If

    Next olMail
    
    
    Set olFolder_Inbox = Nothing
    Set ns = Nothing
    
    Set fso = Nothing
    

End Sub

But when I execute the macro I get roughly (translated from swedish):

Error 76, Cant find the path

What's my mistake?

CodePudding user response:

Looks like it could be either a mistake in the path or a bunch of other stuff as quoted below from a blog post.

Runtime Error 76 will typically show when your computer cannot read a file it requires, either because it's damaged, misplaced or not registered.

Did you try any of the fixes in available posts/guides? Maybe some will work or at least generate useful info, like the one from Microsoft if you haven't tried it yet!

CodePudding user response:

The Subject property of the MailItem class and the FileName property of the Attachment class may contain forbidden symbols that can't be used for filenames. So. before calling the SaveAsFile method of the Attachment class you need to check the file path whether such folder exists and the path doesn't contain forbidden symbols. See What characters are forbidden in Windows and Linux directory names? for more information.

  • Related