vba GetSaveAsFilename 默认文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5148173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:11:04  来源:igfitidea点击:

GetSaveAsFilename default folder

excelvbaexcel-vba

提问by loveforvdubs

I am using GetSaveAsFilenamein 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:\MyDocuments\Musicwhen it is called.

GetSaveAsFilename在 VBA for Excel 中使用。有没有办法给这个默认文件夹打开?例如,我总是希望它在C:\MyDocuments\Music被调用时开始。

采纳答案by Jean-Fran?ois Corbett

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

FileDialog对象提供了比GetSaveAsFilename(及其兄弟GetOpenFilename)更多的灵活性。例子:

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 .InitialFileNamelonger than 256 characters will cause a run-time error.

请注意,.InitialFileName超过 256 个字符将导致运行时错误。

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

请参阅 上的VBA 帮助FileDialog。它有很多有用的属性,包括例如AllowMultiSelect(尽管在保存时这个属性是无关紧要的)。

回答by Andrew Cowenhoven

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.

只到达我的文档并认为我的音乐是文件名。希望这可以帮助。

回答by Mark Ransom

Use ChDirbefore GetSaveAsFilename.

ChDir之前使用GetSaveAsFilename