简体   繁体   English

自动填充只读Excel 2010 xlsm SaveAs文件名框

[英]Autofill Read only Excel 2010 xlsm SaveAs file name box

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

If ThisWorkbook.Name = "Test.xlsm" Then
    Sheets("Sheet1").Select

    If Not IsEmpty(A1.Value) Then

        MsgBox "This workbook is 'read only' Please rename this workbook."
        strName = "Please enter a new file name."
    ThisPAth = Application.ActiveWorkbook.Path
        ThisFile = Range("B1").Value
        ActiveWorkbook.SaveAs Filename:=ThisPath & ThisFile & ".xlsm", FileFormat:=52, CreateBackup:=False
    Else
        MsgBox "Cancelled."
    End If

End If
End Sub

I have a password protected workbook (Test.xlsm" that is strictly for data entry. When the user opens the workbook as read only, enters the data, and then exits the workbook/template, I want the SaveAs dialog box that automatically pops up to have the contents of A1 of Sheet1 to be the "Suggested" file name that is autofilled in the SaveAs box. 我有一个受密码保护的工作簿(Test.xlsm“,严格用于数据输入。当用户以只读方式打开工作簿,输入数据,然后退出工作簿/模板时,我想要自动弹出的SaveAs对话框使Sheet1的A1的内容成为在SaveAs框中自动填充的“建议”文件名。

I thought if I snagged the BeforeSave function that I could declare this path/filename but alas, it does not work. 我想如果我抓住了BeforeSave函数,我可以声明这个路径/文件名但是唉,它不起作用。 The autofill box displays "Copy of Test.xlsm". 自动填充框显示“Copy of Test.xlsm”。 I do not even think it sees the above code. 我甚至认为它没有看到上面的代码。

How can I accomplish autofilling this box with the desired name. 如何使用所需名称完成此框的自动填充。 Thanks. 谢谢。

------------Update------------------ ------------更新------------------

Rewrote code to below but it still is not snagging the default dialog box on save. 将代码重写为下面但仍然没有在保存时阻止默认对话框。 Maybe I am misunderstanding the Workbook_BeforeSave function. 也许我误解了Workbook_BeforeSave功能。 I thought it was automatically called whenever the file is getting saved. 我认为只要文件被保存就会自动调用它。 I never want the user to save the file with the name Test.xltm (I changed file to a template to see if that made a difference) but to suggest the user rename file to the value in B1 for standardization reasons. 我从不希望用户使用名称Test.xltm保存文件(我将文件更改为模板以查看是否有所不同),但建议用户将文件重命名为B1中的值以用于标准化原因。 The code is not getting called automatically. 代码不会自动调用。 I was able to get similar code to work if I call it by executing a macro from the quick access toolbar for example, but cannot seem to get it to execute automatically whenever the user selects "Close", "Save" or "Save As" from the "File" pull down menu. 如果我通过从快速访问工具栏执行宏来调用它,我能够获得类似的代码,但是当用户选择“关闭”,“保存”或“另存为”时,似乎无法自动执行它从“文件”下拉菜单中。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim saveDialog As FileDialog
Debug.Print "Hello"
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)

If ThisWorkbook.Name = "Test.xltm" Then
    Application.EnableEvents = False
    Debug.Print "Save as"
    Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
    With saveDialog
        .InitialFileName = "foo.xlsm"
        .Show
    End With
    Application.EnableEvents = True
Else
    Debug.Print "Cancel"
End If
End Sub

The template is password protected and opened as read only by user so SaveAs dialog box should always open upon exit/save/save as. 模板受密码保护,并由用户只读打开,因此SaveAs对话框应始终在退出/保存/另存为时打开。 Correct? 正确? And shouldn't the Workbook_BeforeSave always get called under this circumstance? 在这种情况下,Workbook_BeforeSave是否应该始终被调用?

Example: 例:

Sub saveDialogTest()
    Dim saveDialog As FileDialog

    Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)

    With saveDialog
        .InitialFileName = "Foo.xlsx"
        .Show
    End With
End Sub

If you use the FileDialog like this, it will allow you to change the suggested file name. 如果您使用这样的FileDialog ,它将允许您更改建议的文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM