简体   繁体   中英

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.

I thought if I snagged the BeforeSave function that I could declare this path/filename but alas, it does not work. The autofill box displays "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. 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. 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. Correct? And shouldn't the Workbook_BeforeSave always get called under this circumstance?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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