Macro to download outlook attachments not working

droot

New Member
Joined
Oct 18, 2018
Messages
36
Office Version
  1. 365
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?


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
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
 
Upvote 0

Forum statistics

Threads
1,214,935
Messages
6,122,337
Members
449,078
Latest member
skydd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top