vba 文件路径控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/264771/
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-11 10:01:51  来源:igfitidea点击:

File Path Control

filevbapathcontrols

提问by Manoj

How to put a File path control in VBA front panel? I want the user to be able to select the browse button and select the file path rather than putting up dialog boxes all over the place. I need the user to select three or more file paths.

如何在 VBA 前面板中放置文件路径控件?我希望用户能够选择浏览按钮并选择文件路径,而不是到处放置对话框。我需要用户选择三个或更多文件路径。

采纳答案by jpinto3912

After re-re-reading your Q, it seams you want to steer away from dialog boxes!Oh well, I was going to say

重新阅读您的 Q 后,您似乎想远离对话框!哦,我正要说

I could post the hack about using MSDIAG on VBA, that explains how you can patch your registry to enable its use under VBA, without having other MS-VB products installed... but I rather have you google that one... you can certainly understand why.

我可以发布关于在 VBA 上使用 MSDIAG 的黑客,它解释了如何修补注册表以使其在 VBA 下使用,而无需安装其他 MS-VB 产品......但我宁愿让你谷歌那个......你可以当然明白为什么。

But you don't want Dialog Boxes... you want controls and buttons: Use listboxes! To populate your listbox, use the Dircommand (using method additemof the listbox). Two phases for achieving that:

但是您不需要对话框...您需要控件和按钮:使用列表框!要填充列表框,请使用Dir命令(使用列表框的additem方法)。实现这一目标的两个阶段:

  • first get the Directories (and prefix a "->" or whatever prior to adding it on the listbox, so that the user understands this is not a file);
  • then get filenames (you can filter by extension with the arguments of Dir, just as you would in DOS).
  • 首先获取目录(并在将其添加到列表框之前添加“->”或其他前缀,以便用户了解这不是文件);
  • 然后获取文件名(您可以使用 Dir 的参数按扩展名过滤,就像在 DOS 中一样)。

Finally, under OnClick and OnDoubleClick of the listbox, you must interpret the listbox default property (Item), check for "->" and use ChDir to change directory and repopulate, or you'll have your file selected.

最后,在列表框的 OnClick 和 OnDoubleClick 下,您必须解释列表框的默认属性 (Item),检查“->”并使用 ChDir 更改目录并重新填充,否则您将选择您的文件。

The write up is sooooooo much more complicated than the code... trust me.

写起来比代码复杂得多......相信我。

回答by fwzgekg

Do you mean VBA for Microsoft Office or just general VBA?

您是指 Microsoft Office 的 VBA 还是一般的 VBA?

In Office, Application.FileDialog(msoFileDialogOpen).

在办公室,Application.FileDialog(msoFileDialogOpen)

Otherwise, look at the Win32 API function SHBrowseForFolder(in shell32.dll). You can import it for use into VBA using the Declare Functionkeywords.

否则,查看 Win32 API 函数SHBrowseForFolder(在 shell32.dll 中)。您可以使用Declare Function关键字将其导入到 VBA 中使用。

回答by Philippe Grondier

There is not direct VBA function for that. You can decide to combine a form (Access form, or a generic microsoft form) with 2 controls: (1) text box (2) browse button (which will finally use the fileDialog command or a windows API).

没有直接的 VBA 功能。您可以决定将表单(Access 表单或通用微软表单)与 2 个控件组合:(1) 文本框 (2) 浏览按钮(最终将使用 fileDialog 命令或 Windows API)。

回答by Fionnuala

Perhaps the browse for folder API from the Microsoft MVPs site would suit:

也许从 Microsoft MVP 站点浏览文件夹 API 会适合:

http://www.mvps.org/access/api/api0002.htm

http://www.mvps.org/access/api/api0002.htm

It uses SHBrowseForFolder mentioned by fwzgekg, and does not return a file dialog, it returns a browsable list of folders.

它使用 fwzgekg 提到的 SHBrowseForFolder,并且不返回文件对话框,它返回一个可浏览的文件夹列表。

回答by nghmuon

Is this what you want?

这是你想要的吗?

FilePath = Application.GetOpenFilename