GetSaveAsFilename default folder

后端 未结 3 1069
伪装坚强ぢ
伪装坚强ぢ 2020-12-11 05:28

I am using GetSaveAsFilename in VBA for Excel. Is there any way to give this a default folder to open up to? For example, I always want it to start at C:\

相关标签:
3条回答
  • 2020-12-11 05:35

    This works:

    x = Application.GetSaveAsFilename(InitialFileName:="C:\mydocuments\music\", _
        fileFilter:="Text Files (*.*), *.*")
    

    However, if you have spaces in the filespec it gets a little trickier. For example, this:

    x = Application.GetSaveAsFilename(InitialFileName:="%USERPROFILE%\My Documents\My Music", _
        fileFilter:="Text Files (*.*), *.*")
    

    only gets as far as My Documents and thinks that My Music is the filename. Hope this helps.

    0 讨论(0)
  • 2020-12-11 05:42

    The FileDialog object offers way more flexibility than GetSaveAsFilename (and its sibling GetOpenFilename). Example:

    Dim tuneSaver As FileDialog
    Set tuneSaver = Application.FileDialog(msoFileDialogSaveAs)
    
    With tuneSaver
        .Title = "Save this tune as..."
        .InitialFileName = "C:\MyDocuments\Music\"
        ' Set other properties here...
        .Show
    End With
    

    Note that an .InitialFileName longer than 256 characters will cause a run-time error.

    See VBA help on FileDialog. It has quite a few useful properties, including e.g. AllowMultiSelect (though admittedly this one is irrelevant when saving).

    0 讨论(0)
  • 2020-12-11 06:01

    Use ChDir before GetSaveAsFilename.

    0 讨论(0)
提交回复
热议问题