vba 如何强制 SaveAs 而不是 Save

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12445757/
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-11 17:43:56  来源:igfitidea点击:

How to force SaveAs instead Save

excelexcel-vbavba

提问by Alegro

I want to prevent user to save the workbook with the same name as it is opened with, and to offer SaveAs option.

我想阻止用户使用与打开时相同的名称保存工作簿,并提供 SaveAs 选项。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If ThisWorkbook.Name = "abc" Then
Cancel = True
SaveAsUI = True
End If

Also tried

也试过

 If ThisWorkbook.Name = "abc" Then SaveAsUI = True

This code doesn't work. SaveAs dialog doesn't appear.

此代码不起作用。不出现另存为对话框。

Next try

下次试试

If ThisWorkbook.Name = "abc" Then ThisWorkbook.ReadOnly = True
'Error - can't assign to read only property.

采纳答案by brettdj

If you want to test for a particular filename only - say abc.xlsmthen the code below will stop the Save(but pass SaveAs) then set the ReadOnlyattribute to False so Savecan't be used again on this file in this session

如果您只想测试特定的文件名 - 假设abc.xlsm下面的代码将停止Save(但通过SaveAs),然后将该ReadOnly属性设置为 False 因此Save不能在此会话中再次用于此文件

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
    If ThisWorkbook.Name = "abc.xlsm" Then
        Cancel = True
        ThisWorkbook.ChangeFileAccess Mode:=xlReadOnly
    End If
End If
End Sub

回答by Olle Sj?gren

I have two suggestions, but in order to know which is best you have to tell us more details about the surrounding code and how you open/create files etc.

我有两个建议,但为了知道哪个最好,您必须告诉我们有关周围代码的更多详细信息以及您如何打开/创建文件等。

  1. Use a template. If you put your code in a template and add a new workbook, it cannot be saved without the SaveAs dialog.

  2. Make the workbook read only at opening. This can be done in a lot of ways, depending on the design of your project (eg Workbooks.Openwith ReadOnlyparameter).

  1. 使用模板。如果将代码放在模板中并添加新工作簿,则在没有“另存为”对话框的情况下无法保存。

  2. 使工作簿在打开时只读。这可以通过多种方式完成,具体取决于您的项目设计(例如Workbooks.Open使用ReadOnly参数)。

回答by chris neilsen

The other answer (read only or template) are both good suggestions

另一个答案(只读或模板)都是很好的建议

However, if you really want to code it, try this

但是,如果你真的想编码,试试这个

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim fName As String
    If ThisWorkbook.Name  "abc.xlsm" Then 
        If Not SaveAsUI Then
            fName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm")
            If fName = "False" Then
                MsgBox "File NOT saved", vbOKOnly
                Cancel = True
            Else
                Application.EnableEvents = False
                ThisWorkbook.SaveAs Filename:=fName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
                Application.EnableEvents = True
            End If
        End If
    End If
End Sub

Note: coded for Excel 2007/2010 (If ThisWorkbook.Name "abc.xlsm" Then)

注:为 Excel 2007/2010 编码 ( If ThisWorkbook.Name "abc.xlsm" Then)

You woill need to change if using Excel 2003

如果使用 Excel 2003,您将需要更改