How to save a file selected in Save As dialog?

后端 未结 2 654
面向向阳花
面向向阳花 2020-12-10 15:17

I found code online that opens a Save As dialog to a location on a drive.

When you click \"save\" the file does not save.

Dim varResult As Variant
\'         


        
2条回答
  •  佛祖请我去吃肉
    2020-12-10 15:55

    I prefer to use the shortest code:

        Application.Dialogs(xlDialogSaveAs).Show ("c:\my_folder\")
    

    It's the standard Excel save dialog.

    It has several parameters (not named), you may need them:

        Dim strFilename As String: strFilename = "report1"
        Dim strFolder As String: strFolder = "C:\temp\" 'initial directory - NOTE: Only works if file has not yet been saved!
        Dim xlfFileFormat As XlFileFormat: xlfFileFormat = XlFileFormat.xlOpenXMLWorkbook 'or replace by other XlFileFormat
        Dim strPassword As String: 'strPassword = "password" 'The password with which to protect the file - if any
        Dim booBackup As Boolean: 'booBackup = True  '(Whether to create a backup of the file.)
        Dim strWriteReservationPassword As String: 'strWriteReservationPassword = "password2" ' (The write-reservation password of the file.)
        Dim booReadOnlyRecommendation As Boolean: booReadOnlyRecommendation = False '(Whether to recommend to the user that the file be opened in read-only mode.)
        Dim booWorkbookSaved As Boolean ' true if file saved, false if dialog canceled
        If Len(strFolder) > 0 Then ChDir strFolder
        booWorkbookSaved = Application.Dialogs(xlDialogSaveAs).Show(Arg1:=strFilename, Arg2:=xlfFileFormat, Arg3:=strPassword, _
                Arg4:=booBackup, Arg5:=strWriteReservationPassword, Arg6:=booReadOnlyRecommendation)
    

提交回复
热议问题