How to use workbook.saveas with automatic Overwrite How to use workbook.saveas with automatic Overwrite vba vba

How to use workbook.saveas with automatic Overwrite


To hide the prompt set xls.DisplayAlerts = False

ConflictResolution is not a true or false property, it should be xlLocalSessionChanges

Note that this has nothing to do with displaying the Overwrite prompt though!

Set xls = CreateObject("Excel.Application")    xls.DisplayAlerts = FalseSet wb = xls.Workbooks.AddfullFilePath = importFolderPath & "\" & "A.xlsx"wb.SaveAs fullFilePath, AccessMode:=xlExclusive,ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges    wb.Close (True)


I recommend that before executing SaveAs, delete the file it exists.

If Dir("f:ull\path\with\filename.xls") <> "" Then    Kill "f:ull\path\with\filename.xls"End If

It's easier than setting DisplayAlerts off and on, plus if DisplayAlerts remains off due to code crash, it can cause problems if you work with Excel in the same session.


To split the difference of opinion

I prefer:

   xls.DisplayAlerts = False       wb.SaveAs fullFilePath, AccessMode:=xlExclusive, ConflictResolution:=xlLocalSessionChanges   xls.DisplayAlerts = True