Ignore the "Do you want to save" field when exiting Excel

I have a script that opens an excel file and runs a macro, and then exits the file. Since the file is in read-only mode and the script makes temporary changes to the file when the script calls myExcelWorker.Quit() excel asks if I want to save my changes and I have to click "no". Is there any way to exit the program and skip this square?

 ' Create a WshShell to get the current directory Dim WshShell Set WshShell = CreateObject("WScript.Shell") ' Create an Excel instance Dim myExcelWorker Set myExcelWorker = CreateObject("Excel.Application") myExcelWorker.Visible = True ' Tell Excel what the current working directory is ' (otherwise it can't find the files) Dim strSaveDefaultPath Dim strPath strSaveDefaultPath = myExcelWorker.DefaultFilePath strPath = WshShell.CurrentDirectory myExcelWorker.DefaultFilePath = strPath ' Open the Workbook specified on the command-line Dim oWorkBook Dim strWorkerWB strWorkerWB = strPath & "\BugHistogram_v2.xlsm" Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB) ' Build the macro name with the full path to the workbook Dim strMacroName strMacroName = "CreateImagesButton_Click" on error resume next ' Run the calculation macro myExcelWorker.Run strMacroName if err.number <> 0 Then ' Error occurred - just close it down. End If err.clear on error goto 0 ' oWorkBook.Save ' this is ignored because it read only myExcelWorker.DefaultFilePath = strSaveDefaultPath ' Clean up and shut down Set oWorkBook = Nothing ' Don't Quit() Excel if there are other Excel instances ' running, Quit() will ' shut those down also if myExcelWorker.Workbooks.Count = 0 Then myExcelWorker.Quit End If myExcelWorker.Quit() Set myExcelWorker = Nothing Set WshShell = Nothing 
+6
source share
2 answers

ActiveWorkbook.Close False (to close the book)

Application.Quit (to exit Excel - does not ask to save changes)

From Microsoft Support How to suppress the Save Changes prompt when closing a workbook in Excel :

To force the book to close without saving any changes, enter the following code in the Visual Basic module of this book:

 Sub Auto_Close() ThisWorkbook.Saved = True End Sub 

Because the Saved property is set to True, Excel responds as if the workbook was already saved and no changes have occurred since the last save.

The DisplayAlerts property of a program can be used for the same purpose. For example, the following macro disables DisplayAlerts , closes the active workbook without saving changes, and then rotates DisplayAlerts again.

 Sub CloseBook() Application.DisplayAlerts = False ActiveWorkbook.Close Application.DisplayAlerts = True End Sub 

You can also use the SaveChanges argument of the Close method.

The following macro closes the book without saving changes:

 Sub CloseBook2() ActiveWorkbook.Close savechanges:=False End Sub 
+7
source

The answer you indicated above is for VBA - you can directly access this in your VBS using

 oWorkBook.Close False Set oWorkBook = Nothing 

instead

 Set oWorkBook = Nothing 
+2
source

Source: https://habr.com/ru/post/971729/


All Articles