vba 浏览文件夹对话框调用的几种方法
来源: 阅读:1220 次 日期:2016-06-29 08:51:26
温馨提示: 小编为您整理了“vba 浏览文件夹对话框调用的几种方法”,方便广大网友查阅!

1、使用api方法

代码如下:

'【类型声明】

private type browseinfo

hwndowner as long

pidlroot as long

pszdisplayname as long

lpsztitle as long

ulflags as long

lpfncallback as long

lparam as long

iimage as long

end type

'【api声明】

private declare function shgetpathfromidlist lib shell32.dll _

alias shgetpathfromidlista (byval pidl as long, _

byval pszpath as string) as long

private declare function shbrowseforfolder lib shell32.dll _

alias shbrowseforfoldera (lpbrowseinfo as browseinfo) as long

private declare function lstrcat lib kernel32 _

alias lstrcata (byval lpstring1 as string, _

byval lpstring2 as string) as long

private declare function oleinitialize lib ole32.dll _

(lp as any) as long

private declare sub oleuninitialize lib ole32 ()

private const bif_usenewui = &h40

private const max_path = 260

'【自定义函数】

public function getfolder_api(stitle as string, optional vflags as variant) as string

dim lpidlist as long

dim sbuffer as string

dim binfo as browseinfo

if ismissing(vflags) then vflags = bif_usenewui

call oleinitialize(byval 0&)

with binfo

.lpsztitle = lstrcat(stitle, )

.ulflags = vflags

end with

lpidlist = shbrowseforfolder(binfo)

if (lpidlist) then

sbuffer = space(max_path)

shgetpathfromidlist lpidlist, sbuffer

sbuffer = left(sbuffer, instr(sbuffer, vbnullchar) - 1)

if sbuffer <> then getfolder_api = sbuffer

end if

call oleuninitialize

end function

'【使用方法】

sub test()

msgbox getfolder_api(选择文件夹)

end sub

2、使用shell.application方法

代码如下:

sub getfloder_shell()

set objshell = createobject(shell.application)

set objfolder = objshell.browseforfolder(0, 选择文件夹, 0, 0)

if not objfolder is nothing then

msgbox objfolder.self.path

end if

set objfolder = nothing

set objshell = nothing

end sub

3、使用filedialog方法

代码如下:

sub getfloder_filedialog()

dim fd as filedialog

set fd = application.filedialog(msofiledialogfolderpicker)

if fd.show = -1 then msgbox fd.selecteditems(1)

set fd = nothing

end sub

以上方法在winxp+office2003中测试通过

更多信息请查看脚本栏目
上一篇:dom样式编程
由于各方面情况的不断调整与变化, 提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 云南省教育厅备案号:云教ICP备0901021 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:
云南网警报警专用图标
Baidu
map