Outlook amend VBA to overwrite if file exists in destination folder

leemcder

New Member
Joined
Feb 26, 2018
Messages
42
Hi, I've used this macro code written by Graham Mayor to save attachments which arrive in to my outlook inbox and save them with the email subject. Is it possible so it can overwrite any documents in the destination folder which has the same file name? Many thanks

VBA Code:
Sub Test()Dim olMsg As MailItem
    On Error Resume Next
    Set olMsg = ActiveExplorer.Selection.Item(1)
    Save_As_Subject olMsg
lbl_Exit:
    Exit Sub
End Sub


Sub Save_As_Subject(olItem As MailItem)
'Graham Mayor - http://www.gmayor.com - Last updated - 13 Jan 2018
Dim olAttach As Attachment
Dim strFileName As String
Dim strExt As String
Const strPath = "Y:\accounts\Conv slips\"    'the path to store the files


    For Each olAttach In olItem.Attachments
        strExt = Mid(olAttach.FileName, InStrRev(olAttach.FileName, Chr(46)))
        Select Case LCase(strExt)
            Case Is = ".docx", ".dotx", ".pdf", ".xlsx", ".zip", ".html", ".jpeg", ".txt", "xls", ".htm", ".doc"  'the wanted extensions
                strFileName = olItem.Subject
                strFileName = CleanFileName(strFileName)
                strFileName = strPath & strFileName & strExt
                olAttach.SaveAsFile strFileName
            Case Else
        End Select
    Next olAttach
lbl_Exit:
    Set olItem = Nothing
    Set olAttach = Nothing
    Exit Sub
End Sub


Private Function CleanFileName(strFileName As String) As String
     'Graham Mayor - http://www.gmayor.com - Last updated - 05 Jul 2017
     'A function to ensure there are no illegal filename
     'characters in a string to be used as a filename
    Dim arrInvalid() As String
    Dim vfName As Variant
    Dim lng_Name As Long
    Dim lng_Ext As Long
    Dim lngIndex As Long
    CleanFileName = strFileName
     'Define illegal characters (by ASCII CharNum)
    arrInvalid = Split("9|10|11|13|34|42|47|58|60|62|63|92|124", "|")
     'Add the extension to the filename
    CleanFileName = CleanFileName
     'Remove any illegal filename characters
    For lngIndex = 0 To UBound(arrInvalid)
        CleanFileName = Replace(CleanFileName, Chr(arrInvalid(lngIndex)), Chr(95))
    Next lngIndex
lbl_Exit:
    Exit Function
End Function
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
This new line of code should do:
VBA Code:
'...
strFileName = strPath & strFileName & strExt
If (Dir$(strFileName) <> "") Then Kill strFileName '<- added (delete file if already exists)
olAttach.SaveAsFile strFileName
'...
By the way, what if you have more than one attachment in the email ? have you considered the case ?
 
Last edited:
Upvote 0
This new line of code should do:
VBA Code:
'...
strFileName = strPath & strFileName & strExt
If (Dir$(strFileName) <> "") Then Kill strFileName '<- added (delete file if already exists)
olAttach.SaveAsFile strFileName
'...
Thank you, its much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,098
Members
453,021
Latest member
Justyna P

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