Outlook VBA: Save attachment, open excel and run a macro.

s0342889

New Member
Joined
Aug 25, 2016
Messages
2
Hi

I'm relatively new to VBA. Trying to use a script in Outlook that will save a specific excel attachment when it arrives, then open it and run a macro contained in the workbook. Here is the code I have now. It works but the problem is that it will not overwrite the file in the save location. When I try sending an updated version, it doesn't save it and opens the old version instead.

Please help!

Public Sub SaveAttachments(Item As Outlook.MailItem)


If Item.Attachments.Count > 0 Then

Dim objAttachments As Outlook.Attachments
Dim lngCount As Long
Dim strFile As String
Dim sFileType As String
Dim i As Long


Set objAttachments = Item.Attachments
lngCount = objAttachments.Count
For i = lngCount To 1 Step -1


' Get the file name.
strFile = objAttachments.Item(i).FileName


' Get the path to your My Documents folder
strfolderpath = "*******"


' Combine with the path to the folder.
strFile = strfolderpath & strFile


' Save the attachment as a file.
objAttachments.Item(i).SaveAsFile strFile


Next i
End If


On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then Set xlApp = CreateObject("Excel.Application")
On Error GoTo 0
xlApp.Visible = True


On Error Resume Next
Set xlWB2 = xlApp.Workbooks("*********") 'Replace yourfilename with the name of your macro workbook
If xlWB2 Is Nothing Then Set xlWB2 = xlApp.Workbooks.Open("***********") 'Replace yourpath with the path to your macro workbook
On Error GoTo 0


xlWB2.Application.Run ("'**********'!Macro_name")


End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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