0

I have been trying to find a way to set up the default save options for a specific excel spreadsheet where a default file location is opened, a suggested file name appears in the dialog box and the default file type is a macro enabled workbook(*.xlsm).

I have complied the following two solutions from research; however, both seem to give me a problem where three save dialog boxes open after each other.
Specifically, when the save button is clicked, the save dialog box opens in the correct location (“My Documents/exceltests”) and with the correct file name (“AAAA”). I then click the save button which prompts another save dialog box to open with the exact same properties. After I click save on this dialog box, the workbook is saved in the correct location. However, a third dialog box also opens back in the default location of “My Documents”.

Code Sample 1

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
Cancel As Boolean)
With Application.FileDialog(msoFileDialogSaveAs)


.InitialFileName = "C:\My Documents\exceltests\AAAA"
 .FilterIndex = 2

 If .Show Then
     ActiveWorkbook.SaveAs Filename:=.SelectedItems(1), _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled
End If
End With

End Sub

Code Sample 2

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

    End Sub
    Dim fn


fn = Application.GetSaveAsFilename(InitialFileName:="C:\My Documents\exceltests\AAAA", _
fileFilter:=" Excel Macro Enabled Workbook (*.xlsm), *.xlsm,")

If fn <> False Then
ActiveWorkbook.SaveAs Filename:=fn, _
    FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

End If

If anyone has any clues as to why this is happening and/or how it can be fixed I would be very grateful.

1 Answer 1

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

With Application.FileDialog(msoFileDialogSaveAs)
    Application.EnableEvents = False
    .InitialFileName = "C:\My Documents\exceltests\AAAA"
    .FilterIndex = 2
    If .Show Then
        ActiveWorkbook.SaveAs Filename:=.SelectedItems(1), _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled
    End If
    Application.EnableEvents = True
End With

Cancel = True

End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This almost works. It seems to have got rid of the second dialog box; however, the third one (showing the default My Documents) still comes up. So only two dialog boxes now. Any ideas as to why this is happening?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.