Dear all
I want to do a Vba/Macro within outlook to download the the attachments (Pdf and csv) from a particular outlook folder to my document file. I tried to attempt one of the threads but then it didnt work and I'm sure I've mucked it up . Below is the one I used but got error. The outlook folder is called SW UK and I want the attachments of all unread emails to be saved to the path "C:\Users\RG\Documents\RG\Weekly Statement\Last week". After I run the VBA, it is giving me error as below
Run-Time Error '-2147221233 (8004010f)
Below is the Code I've used
Const olFolderInbox As Integer = 6
'~~> Path for the attachment
Const AttachmentPath As String = "C:\Users\RG\Documents\RG\Weekly Statement\Last week"
Sub DownloadAttachmentFirstUnreadEmail()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object, oOlAtch As Object
'~~> New File Name for the attachment
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox).Parent
Set oOlInb = oOlInb.Folders("Subfolder1").Folders("Subfolder1_subfolder1").Folders("SW UK")
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Extract the attachment from the 1st unread email
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
'~~> Check if the email actually has an attachment
If oOlItm.Attachments.Count <> 0 Then
For Each oOlAtch In oOlItm.Attachments
'~~> Download the attachment
oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
Exit For
Next
Else
MsgBox "The First item doesn't have an attachment"
End If
Exit For
Next
End Sub
Please kindly help
Thanks for reading guys
Best Regards
R
I want to do a Vba/Macro within outlook to download the the attachments (Pdf and csv) from a particular outlook folder to my document file. I tried to attempt one of the threads but then it didnt work and I'm sure I've mucked it up . Below is the one I used but got error. The outlook folder is called SW UK and I want the attachments of all unread emails to be saved to the path "C:\Users\RG\Documents\RG\Weekly Statement\Last week". After I run the VBA, it is giving me error as below
Run-Time Error '-2147221233 (8004010f)
Below is the Code I've used
Const olFolderInbox As Integer = 6
'~~> Path for the attachment
Const AttachmentPath As String = "C:\Users\RG\Documents\RG\Weekly Statement\Last week"
Sub DownloadAttachmentFirstUnreadEmail()
Dim oOlAp As Object, oOlns As Object, oOlInb As Object
Dim oOlItm As Object, oOlAtch As Object
'~~> New File Name for the attachment
Dim NewFileName As String
NewFileName = AttachmentPath & Format(Date, "DD-MM-YYYY") & "-"
'~~> Get Outlook instance
Set oOlAp = GetObject(, "Outlook.application")
Set oOlns = oOlAp.GetNamespace("MAPI")
Set oOlInb = oOlns.GetDefaultFolder(olFolderInbox).Parent
Set oOlInb = oOlInb.Folders("Subfolder1").Folders("Subfolder1_subfolder1").Folders("SW UK")
'~~> Check if there are any actual unread emails
If oOlInb.Items.Restrict("[UnRead] = True").Count = 0 Then
MsgBox "NO Unread Email In Inbox"
Exit Sub
End If
'~~> Extract the attachment from the 1st unread email
For Each oOlItm In oOlInb.Items.Restrict("[UnRead] = True")
'~~> Check if the email actually has an attachment
If oOlItm.Attachments.Count <> 0 Then
For Each oOlAtch In oOlItm.Attachments
'~~> Download the attachment
oOlAtch.SaveAsFile NewFileName & oOlAtch.Filename
Exit For
Next
Else
MsgBox "The First item doesn't have an attachment"
End If
Exit For
Next
End Sub
Please kindly help
Thanks for reading guys
Best Regards
R
Last edited: