Default Folder GetSaveAsFilename

I am using GetSaveAsFilename in VBA for Excel. Is there any way to open this folder by default? For example, I always want it to start with C:\MyDocuments\Music when it is called.

+3
source share
3 answers

The FileDialog object offers 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 .InitialFileName longer than 256 characters will result in a run-time error.

See VBA Help for FileDialog . It has quite a few useful properties, including, for example, AllowMultiSelect (although, admittedly, it does not matter when saving).

+2
source

Use ChDir before GetSaveAsFilename .

+3
source

It works:

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

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

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

just gets to My Documents and thinks My Music is the file name. Hope this helps.

+3
source

Source: https://habr.com/ru/post/914678/


All Articles