强制另存为,MS Excel VBA

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

Force Save As, MS Excel VBA

excelvba

提问by Mike

I'm a bit of a newby with VBA in MS Office products. I've been searching for some code that will force a user to "Save As' a .xls file I use as a template (but it's not an actual template file type)

我是 MS Office 产品中 VBA 的新手。我一直在寻找一些代码来强制用户“另存为”我用作模板的 .xls 文件(但它不是实际的模板文件类型)

Basically, I have this:

基本上,我有这个:

  1. User opens .xls, enters some data into some field and then File-->Save As to their own location
  2. Sometimes user clicks save instead, therefore overwriting the .xls which I don't want to happen.
  1. 用户打开 .xls,在某个字段中输入一些数据,然后 File-->Save As 到他们自己的位置
  2. 有时用户会点击保存,因此会覆盖我不想发生的 .xls。

I've been looking into some ideas, but I'm not sure how to implement it the best. I think a prompt when the user first opens the .xls to save to a new location is the best approach, but thinking forward, if they have already saved the file to a new location and decide to edit the new one, I want them to be able to 'Save' at that point because now it is their own file, not the original.

我一直在研究一些想法,但我不确定如何最好地实施它。我认为当用户第一次打开 .xls 以保存到新位置时的提示是最好的方法,但想想看,如果他们已经将文件保存到新位置并决定编辑新位置,我希望他们能够在那时“保存”,因为现在它是他们自己的文件,而不是原始文件。

If someone can point me in the right direction or find flaws in my logic, I'd love to hear it.

如果有人能指出我正确的方向或发现我的逻辑缺陷,我很乐意听到。

Thanks, Mike

谢谢,迈克

回答by Siddharth Rout

I use as a template (but it's not an actual template file type)

我用作模板(但它不是实际的模板文件类型)

The most simplest way is to save the file with Read-Only Recommendedset to true. See the snapshot

最简单的方法是将文件保存为Read-Only Recommended设置为true。查看快照

enter image description here

在此处输入图片说明

That ways even if the user tries to do a Save, Excel will automatically prompt for a Save As

这样,即使用户尝试执行Save,Excel 也会自动提示输入Save As

enter image description here

在此处输入图片说明

HTH

HTH

回答by Dick Kusleika

I agree with the others that templates and read-only are better options, but if you're set on rolling your own, here's an example to get you started. It goes in the ThisWorkbook module

我同意其他人的观点,即模板和只读是更好的选择,但如果您准备自己动手,这里有一个示例可以帮助您入门。它进入 ThisWorkbook 模块

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim sNewName As String

    Cancel = True 'Cancel the save operation

    sNewName = Application.GetSaveAsFilename(Replace(Me.Name, ".xls", "1.xls"))

    If sNewName <> Me.FullName And sNewName <> "False" Then
        Application.EnableEvents = False
            Me.SaveAs sNewName
        Application.EnableEvents = True
    End If

End Sub