在VBA代码中实现浏览文件夹功能


有两种常用的打开对话框浏览文件夹功能。它们都是直接调用的Windows系统中的Shell。方法如下:

【方法一】:先在您的MVBA项目中引用Microsoft Shell And Automation,然后键入如下代码:

Dim myShell As New Shell32.Shell
Dim myRootFolder As Shell32.Folder3
Set myRootFolder = myShell.BrowseForFolder(0, "Pick", 0)

【方法二】:此法不需要事先引用任何COM组件,而是在代码中通过CreateObject临时创建对象。

Dim objShell As Object, objFolder As Object
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "Example", 1, "d:\")
MsgBox objFolder.Self.Path
MsgBox myRootFolder.Self.Path