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
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