VBA 工作簿。如果用户要保存,则在不提示的情况下关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11220537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 13:24:12  来源:igfitidea点击:

VBA workbooks.Close without being prompted to if the user wants to save?

excelvba

提问by power

I am writing a VBA program that converts .xlsfiles into .csvfiles. The problem is that is brings up the "Do you want to save the changes to myFile.csv?"Dialog box.

我正在编写一个将.xls文件转换为.csv文件的 VBA 程序。问题是弹出"Do you want to save the changes to myFile.csv?"对话框。

Here is a snippet of my code:

这是我的代码片段:

currentBook.SaveAs Filename:=fileNameS, FileFormat:=xlCSV, ConflictResolution:=xlLocalSessionChanges    
currentBook.Close SaveChanges:=False

What do I need to add so that I don't get the "Do you want to save the changes to myFile.csv?" Dialog box?

我需要添加什么,以免收到“您要将更改保存到myFile.csv?”的提示。对话框?

采纳答案by Alistair Weir

Try using ThisWorkbook.Saved = True

尝试使用 ThisWorkbook.Saved = True

        Application.DisplayAlerts = False
        currentBook.SaveAs Filename:=fileNameS, FileFormat:=xlCSV, ConflictResolution:=xlLocalSessionChanges
        currentBook.Saved = True            
        currentBook.Close SaveChanges:=False
        Application.DisplayAlerts = True

回答by Dan

This is what I have done in the past and it's worked for me:

这是我过去所做的,它对我有用:

Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=fileNameS, FileFormat:=xlCSV, conflictResolution:=xlLocalSessionChanges
Application.DisplayAlerts = True
ActiveWorkbook.Close False

You can also try putting the Close before the DisplayAlerts is reset to true...

您还可以尝试在 DisplayAlerts 重置为 true 之前关闭 Close ...