Macro to download outlook attachments not working

droot

New Member
Joined
Oct 18, 2018
Messages
27
Hi,

I need to download lots of attachments from various folders in outlook and trying to use the code below. The issue is that on some folders it works just fine but for others i get a runtime error 13 on the line in red. Any ideas on why that's happening?

Also, is it possible to ammend the code so that it downloads the attachments from the main folder, sub folders and sub sub folders?


Sub download_attachments()

Dim olapp As Outlook.Application
Dim olmail As MailItem
Dim Att As Object
Dim olfolder As Outlook.Folder
Dim namap As NameSpace

strfolderpath = "U:\test_folder"

Set olapp = CreateObject("Outlook.application")
Set olmail = olapp.CreateItem(olMailItem)
Set olfolder = olapp.GetNamespace("MAPI").PickFolder

For Each olmail In olfolder.Items

If TypeName(olmail) = "MailItem" Then

y = 1
For Each Att In olmail.Attachments

strfile = olmail.Attachments.Item(y).FileName
strfile = strfolderpath & strfile

olmail.Attachments.Item(y).SaveAsFile strfile

y = y + 1

Next Att

Else
Exit Sub

End If

Next

End Sub

credit - https://www.youtube.com/watch?v=NMAkhIcTW3A
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,593
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I need to download lots of attachments from various folders in outlook and trying to use the code below. The issue is that on some folders it works just fine but for others i get a runtime error 13 on the line in red. Any ideas on why that's happening?

Also, is it possible to ammend the code so that it downloads the attachments from the main folder, sub folders and sub sub folders?




credit - https://www.youtube.com/watch?v=NMAkhIcTW3A

Try this

Code:
Sub download_attachments()
    Dim olApp As Outlook.Application
    Dim olmail As MailItem
    Dim Att As Object
    Dim olFolder As Outlook.Folder
    Dim namap As Namespace
    Dim subfolder As Outlook.Folder
    
    strfolderpath = "U:\test_folder\"


    Set olApp = CreateObject("Outlook.application")
    Set olmail = olApp.CreateItem(olMailItem)
    Set olFolder = olApp.GetNamespace("MAPI").PickFolder
    
    For Each subfolder In olFolder.Folders
        On Error Resume Next
        For Each olmail In subfolder.Items
            If TypeName(olmail) = "MailItem" Then
                y = 1
                For Each Att In olmail.Attachments
                    strfile = olmail.Attachments.Item(y).Filename
                    strfile = strfolderpath & strfile
                    olmail.Attachments.Item(y).SaveAsFile strfile
                    y = y + 1
                Next Att
            'Else
            '    Exit Sub
            End If
        Next
    Next
    MsgBox "Done"
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,109,013
Messages
5,526,268
Members
409,689
Latest member
martin_br

This Week's Hot Topics

Top