忽略excel退出时的“你想保存”框
我有一个脚本,打开一个Excel文件并运行一个macros,然后退出该文件。 由于文件处于只读模式,并且脚本对文件进行了临时更改,因此当脚本调用myExcelWorker.Quit()
excel时,询问是否要保存更改,并且必须单击“否”。 有什么办法可以退出程序并跳过这个框?
' 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's 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
ActiveWorkbook.Close False
(closures工作簿)
Application.Quit
(退出Excel – 不提示保存更改)
从Microsoft支持如何在Excel中closures工作簿时禁止“保存更改”提示 :
要强制工作簿closures而不保存任何更改,请在该工作簿的Visual Basic模块中键入以下代码:
Sub Auto_Close() ThisWorkbook.Saved = True End Sub
因为Saved属性设置为True,所以Excel响应,就像工作簿已经保存,并且自上次保存以来没有发生任何更改。
该程序的DisplayAlerts属性可用于相同的目的。 例如,下面的macrosclosuresDisplayAlerts ,closures活动工作簿而不保存更改,然后再次打开DisplayAlerts 。
Sub CloseBook() Application.DisplayAlerts = False ActiveWorkbook.Close Application.DisplayAlerts = True End Sub
您也可以使用Close方法的SaveChanges参数。
下面的macros在不保存更改的情况下closures工作簿:
Sub CloseBook2() ActiveWorkbook.Close savechanges:=False End Sub
你上面的答案是VBA
– 你可以直接在你的VBS
使用
oWorkBook.Close False Set oWorkBook = Nothing
代替
Set oWorkBook = Nothing