gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:33:16 25 4
gpt4 key购买 nike

我有一个脚本,可以打开 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com