Help please Extract Attachments from MS Outlook to save to specific folder

dwilson38550m

Board Regular
Joined
Nov 21, 2005
Messages
89
Hi,

I am going crazy trying to get this to work. I wonder if someone could give me some help. Every day I receive a file statement from the bank showing daily bank balances - I have created a rule in MS Outlook (2016) to move these emails containing an attachment to a folder called HSBC. I would like to use VBA in Excel to automatically extract the file attachments and save these to a specific folder C:\Users\david.wilson\Desktop\HSBC. The files will
- always have IntradayStatement in the filename;
- the email will always come from the sender address which has HSBCnet in email address and
- the subject will always be HSBCnet Automated File Delivery.

I want to save the file attached with the filename "HSBCdownload.xls" to C:\Users\david.wilson\Desktop\HSBC which I have specified on cell A3 of the current workbook (the idea is that the files downloaded daily will overwrite the existing files in this folder)...so I always see the most current version.

I have attached the code I have created so far but this doesn't seem to work - is there anything anyone can suggest? Thanks in advance.
-----------------------------------------------------------------------------------------------------------------------------------------

Option Explicit

' early binding
Sub outlook_email_save()
Dim olook As Outlook.Application
Set olook = New Outlook.Application

Dim omail As Outlook.MailItem
Set omail = olook.CreateItem(olMailItem)

Dim ospace As Outlook.Namespace
Set ospace = olook.GetNamespace("MAPI")

Dim myfol As Outlook.Folder
Set myfol = ospace.GetDefaultFolder(olFolderInbox).Folders("HSBC")
Dim atmt As Attachment

For Each omail In myfol.Items

If omail.Subject Like "HSBCnet Automated" Then
For Each atmt In omail.Attachments

If atmt.Filename Like "*" & "IntradayStatement" Then
atmt.SaveAsFile Range("A3").Value & atmt.Filename
Else
'do nothing
End If
Next


Else
'do nothing
End If

Next


End Sub


Thanks in advance.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,215,467
Messages
6,124,984
Members
449,201
Latest member
Lunzwe73

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