vba - 退出 Excel 时忽略 "Do you wish to save"框

标签 vba excel vbscript

我有一个脚本,可以打开 Excel 文件并运行宏,然后退出该文件。由于文件处于只读模式,并且脚本对文件进行了临时更改,因此当脚本调用 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(关闭工作簿)

Application.Quit(退出 Excel - 不提示保存更改)

来自 Microsoft 支持的 How to suppress "Save Changes" prompt when you close a workbook in Excel :

To force a workbook to close without saving any changes, type the following code in a Visual Basic module of that workbook:

Sub Auto_Close()
    ThisWorkbook.Saved = True
End Sub

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

The DisplayAlerts property of the program can be used for the same purpose. For example, the following macro turns DisplayAlerts off, closes the active workbook without saving changes, and then turns DisplayAlerts on 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 workbook without saving changes:

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

关于vba - 退出 Excel 时忽略 "Do you wish to save"框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24563007/

相关文章:

c# - 在 C# 中更改 Excel Power Query 连接字符串

vba - 为什么我无法将工作簿定义为对象?

vbscript - 如何使用 vbscript 编写事件日志

ms-access - 微软 Access : No value given for one or more required parameters

excel - 使用 VBA 缩放几列以适合页面

sql-server - 如何在Access VBA中检查ODBC SQL Server表是否具有写 Access 权限?

R 中 difftime 的结果与 excel 和 timeanddate.com 不同

excel - 通过 VBScript 从主机名列表更新 Excel 连续 ping

excel - 将每个数据集转换为 1 行

vba - 如何删除 Word.Selection?