DominicD74
DominicD74

Reputation: 157

Overwrite previously saved attachment

I have save attachment VBA code for Outlook. It doesn't process if I receive an attachment with the same name. How do overwrite and save the latest?

Sub ExportAttach(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "C:\Users\Redirection\johndoe\Desktop\TestFolder"
For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
    Set objAtt = Nothing
Next
End Sub

Upvotes: 0

Views: 3780

Answers (1)

Krish
Krish

Reputation: 5917

One method would be: construct the destination file name first, check if the file exists, if yes delete the file and then save the file.

Dim FN as String
FN = SaveFolder & "\" & objAtt.DisplayName

if (Dir$(fn) <> "") then kill fn ' delete if file exists

objAtt.saveAsFile fn

Upvotes: 4

Related Questions