博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转载】VBA:调用文件夹对话框的几种方法
阅读量:6368 次
发布时间:2019-06-23

本文共 1897 字,大约阅读时间需要 6 分钟。

一、使用API方法 

'【类型声明】Private Type BROWSEINFOhWndOwner As LongpIDLRoot As LongpszDisplayName As LonglpszTitle As LongulFlags As LonglpfnCallback As LonglParam As LongiImage As LongEnd Type'【API声明】Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _Alias "SHGetPathFromIDListA" (ByVal pidl As Long, _ByVal pszPath As String) As LongPrivate Declare Function SHBrowseForFolder Lib "shell32.dll" _Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As LongPrivate Declare Function lstrcat Lib "kernel32" _Alias "lstrcatA" (ByVal lpString1 As String, _ByVal lpString2 As String) As LongPrivate Declare Function OleInitialize Lib "ole32.dll" _(lp As Any) As LongPrivate Declare Sub OleUninitialize Lib "ole32" ()Private Const BIF_USENEWUI = &H40Private Const MAX_PATH = 260'【自定义函数】Public Function GetFolder_API(sTitle As String, Optional vFlags As Variant) As StringDim lpIDList As LongDim sBuffer As StringDim BInfo As BROWSEINFOIf IsMissing(vFlags) Then vFlags = BIF_USENEWUICall OleInitialize(ByVal 0&)With BInfo.lpszTitle = lstrcat(sTitle, "").ulFlags = vFlagsEnd WithlpIDList = SHBrowseForFolder(BInfo)If (lpIDList) ThensBuffer = Space(MAX_PATH)SHGetPathFromIDList lpIDList, sBuffersBuffer = Left(sBuffer, InStr(sBuffer, vbNullChar) - 1)If sBuffer <> "" Then GetFolder_API = sBufferEnd IfCall OleUninitializeEnd Function'【使用方法】Sub Test()MsgBox GetFolder_API("选择文件夹")End Sub

 

二、Shell.Application方法

Sub GetFloder_Shell()Set objShell = CreateObject("Shell.Application")Set objFolder = objShell.BrowseForFolder(0, "选择文件夹", 0, 0)If Not objFolder Is Nothing ThenMsgBox objFolder.self.pathEnd IfSet objFolder = NothingSet objShell = NothingEnd Sub

 

三、使用FileDialog方法

Sub GetFloder_FileDialog()Dim fd As FileDialogSet fd = Application.FileDialog(msoFileDialogFolderPicker)If fd.Show = -1 Then MsgBox fd.SelectedItems(1)Set fd = NothingEnd Sub

 

转载于:https://www.cnblogs.com/yl153/articles/6727195.html

你可能感兴趣的文章
Jvm(22),回收策略-----标记清除算法
查看>>
MySQL多表关联查询效率高点还是多次单表查询效率高,为什么?
查看>>
UNIX 高手的 10 个习惯
查看>>
传值与传引用
查看>>
HDU 1538 A Puzzle for Pirates(海盗分金问题)
查看>>
C# Web Forms - Using jQuery FullCalendar
查看>>
H5移动端知识点总结
查看>>
Sublime-Text-2-pydocstring --- 自动生成python docstring的插件
查看>>
UNIX进程环境
查看>>
学习面试题Day03
查看>>
我最喜欢的jQuery插件模板
查看>>
【云计算】Docker 多进程管理方案
查看>>
[LeetCode] Best Meeting Point 最佳开会地点
查看>>
基于InstallShield2013LimitedEdition的安装包制作
查看>>
【转】从Shell脚本内部将所有标准输出及标准错误显示在屏幕并同时写入文件的方法...
查看>>
iOS开发小技巧--利用MJExtension解决数据结构复杂的模型转换
查看>>
Python中的图形库
查看>>
Linux操作系统分析 ------------------中国科技大学
查看>>
Apache多站点实现原理和配置
查看>>
javascript类型系统——包装对象
查看>>