COM .SaveAs Overwrite file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

COM .SaveAs Overwrite file

Post by ShubhamM » 30 Nov 2017, 15:40

Hey, everyone,

I want to use COM .SaveAs on a excel file but I do not want to be prompted on whether I want to save the file but I simply want it to overwrite the existing file. Is there any way to do this?

Thanks!

User avatar
Gio
Posts: 1248
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: COM .SaveAs Overwrite file

Post by Gio » 30 Nov 2017, 17:34

Hello ShubhamM.

You can disable warnings by setting DisplayAlerts to false.

Example (Code below DOES NOT prompt a overwrite warning):

Code: Select all

Xl := ComObjCreate("Excel.Application")
Xl.Workbooks.Add 				        ;add a new workbook w/ standard 3 sheets
Xl.Visible := True
Xl.DisplayAlerts := False
Xl.ActiveWorkbook.SaveAs(A_SCriptDir . "\UniquelyNamedSheet", 51)
If you remove the line Xl.DisplayAlerts := False IT WILL prompt an overwrite alert if there is a file with that name. Also, if AutoHotkey lacks permission to modify the file, this will prompt an error (Example: if the file is already opened in excel and you try to save some other file with that name while it is open you will get an error and the file WILL NOT be saved).

Best wishes.

ShubhamM
Posts: 38
Joined: 08 Nov 2017, 21:38

Re: COM .SaveAs Overwrite file

Post by ShubhamM » 30 Nov 2017, 18:09

You're the best, Gio! Thanks a lot for this! It works like a charm!

Post Reply

Return to “Ask for Help (v1)”