乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 94|回复: 3

64位VBA 7.1中的“浏览文件夹”系统对话框

[复制链接]

7

主题

100

帖子

7

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2014-8-7 01:33:50 | 显示全部楼层 |阅读模式
此模块将允许您在64位VBA 7.1中使用Windows系统SHBrowseForFolder对话框
  1. '##########################################################################################
  2. '#                                                                                        #
  3. '#                                modFolderBrowse                                         #
  4. '#                               code compiled by                                         #
  5. '#                                Steven Elliott                                          #
  6. '#                          from many sources on the net                                  #
  7. '#                           Released to Public Domain                                    #
  8. '#                                                                                        #
  9. '##########################################################################################
  10. Option Explicit
  11.         
  12. Private Declare PtrSafe Function SHBrowseForFolder Lib "shell32.dll" _
  13.   Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) As Long
  14.   
  15. Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32.dll" _
  16.         (ByVal pidList As Long, ByVal lpBuffer As String) As Long
  17.   
  18. Public Declare PtrSafe Function SendMessageA Lib "user32" _
  19.       (ByVal hWnd As LongPtr, ByVal wMsg As Long, _
  20.        ByVal wParam As LongPtr, lParam As Any) As LongPtr
  21.   
  22. Private Declare PtrSafe Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
  23. Private Const BIF_RETURNONLYFSDIRS  As Long = 1
  24. Private Const CSIDL_DRIVES          As Long = &H11
  25. Private Const WM_USER               As Long = &H400
  26. Private Const MAX_PATH              As Long = 260 ' Is it a bad thing that I memorized this value?
  27. '// message from browser
  28. Private Const BFFM_INITIALIZED     As Long = 1
  29. Private Const BFFM_SELCHANGED      As Long = 2
  30. Private Const BFFM_VALIDATEFAILEDA As Long = 3 '// lParam:szPath ret:1(cont),0(EndDialog)
  31. Private Const BFFM_VALIDATEFAILEDW As Long = 4 '// lParam:wzPath ret:1(cont),0(EndDialog)
  32. Private Const BFFM_IUNKNOWN        As Long = 5 '// provides IUnknown to client. lParam: IUnknown*
  33. '// messages to browser
  34. Private Const BFFM_SETSTATUSTEXTA   As Long = WM_USER + 100
  35. Private Const BFFM_ENABLEOK         As Long = WM_USER + 101
  36. Private Const BFFM_SETSELECTIONA    As Long = WM_USER + 102
  37. Private Const BFFM_SETSELECTIONW    As Long = WM_USER + 103
  38. Private Const BFFM_SETSTATUSTEXTW   As Long = WM_USER + 104
  39. Private Const BFFM_SETOKTEXT        As Long = WM_USER + 105 '// Unicode only
  40. Private Const BFFM_SETEXPANDED      As Long = WM_USER + 106 '// Unicode only
  41.         
  42. Public Type BrowseInfo
  43.   hWndOwner As LongPtr
  44.   pIDLRoot As Long
  45.   pszDisplayName As String
  46.   lpszTitle As String
  47.   ulFlags As Long
  48.   lpfnCallback As LongPtr
  49.   lParam As LongPtr
  50.   iImage As Long
  51. End Type
  52. Private Function PtrToFunction(ByVal lFcnPtr As LongPtr) As LongPtr
  53.   PtrToFunction = lFcnPtr
  54. End Function
  55. Private Function CorrectPath(ByVal sPath As String) As String
  56.   If Right$(sPath, 1) = "" Then
  57.     If Len(sPath) > 3 Then sPath = Left$(sPath, Len(sPath) - 1) ' Strip backslash from non-root
  58.   Else
  59.     If Len(sPath) = 2 Then sPath = sPath & ""                  ' Append backslash to root
  60.   End If
  61.   CorrectPath = sPath
  62. End Function
  63. Public Function FolderBrowse(ByVal sDialogTitle As String, ByVal sPath As String) As String
  64.   Dim ReturnPath As String
  65.   
  66.   Dim b(MAX_PATH) As Byte
  67.   Dim pItem       As Long
  68.   Dim sFullPath   As String
  69.   Dim bi          As BrowseInfo
  70.   Dim ppidl       As Long
  71.   
  72.   sPath = CorrectPath(sPath)
  73.   
  74.   bi.hWndOwner = 0 'Screen.ActiveForm.hwnd
  75.   'SHGetSpecialFolderLocation bi.hWndOwner, CSIDL_DRIVES, ppidl
  76.   bi.pIDLRoot = 0 'ppidl
  77.   bi.pszDisplayName = VarPtr(b(0))
  78.   bi.lpszTitle = sDialogTitle
  79.   bi.ulFlags = BIF_RETURNONLYFSDIRS
  80.   If FolderExists(sPath) Then bi.lpfnCallback = PtrToFunction(AddressOf BFFCallback)
  81.   bi.lParam = StrPtr(sPath)
  82.   pItem = SHBrowseForFolder(bi)
  83.   
  84.   If pItem Then ' Succeeded
  85.     sFullPath = Space$(MAX_PATH)
  86.     If SHGetPathFromIDList(pItem, sFullPath) Then
  87.       ReturnPath = Left$(sFullPath, InStr(sFullPath, vbNullChar) - 1) ' Strip nulls
  88.       CoTaskMemFree pItem
  89.     End If
  90.   End If
  91.   
  92.   If Right$(ReturnPath, 1)  "" And ReturnPath  "" Then  'Could be "C:"
  93.     FolderBrowse = ReturnPath & ""
  94.   End If
  95.   
  96. End Function
  97. Public Function BFFCallback(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal sData As String) As Long
  98.   If uMsg = BFFM_INITIALIZED Then
  99.     SendMessageA hWnd, BFFM_SETSELECTIONA, True, ByVal sData
  100.   End If
  101. End Function
  102. Public Function FolderExists(ByVal sFolderName As String) As Boolean
  103.    Dim att As Long
  104.    On Error Resume Next
  105.    att = GetAttr(sFolderName)
  106.    If Err.Number = 0 Then
  107.       FolderExists = True
  108.    Else
  109.       Err.Clear
  110.       FolderExists = False
  111.    End If
  112.    On Error GoTo 0
  113. End Function

将其用于以下简单调用:
  1. Private Sub cmdGetLibraryPath_Click()
  2.    Dim sPath As String
  3. '                          FolderBrowse(DialogBoxTitle, StartingPath)
  4.    sPath = modFolderBrowse.FolderBrowse("Select Folder Containing Block Library", txtLibraryPath.Text)
  5.    If sPath  "" Then
  6.       If modFolderBrowse.FolderExists(sPath) Then
  7.          txtLibraryPath.Text = sPath
  8.       End If
  9.    End If
  10. End Sub

回复

使用道具 举报

154

主题

1274

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1936
发表于 2015-4-29 11:28:21 | 显示全部楼层
It#039;太棒了
你能做到吗;opendialog“;和“;savedialog“;在64位VBA7.1中?
回复

使用道具 举报

7

主题

100

帖子

7

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
135
发表于 2020-9-29 17:36:58 | 显示全部楼层
代码在这一行上与AutoCAD崩溃:如果SHGetPathFromIDList(pItem,sFullPath),则
回复

使用道具 举报

154

主题

1274

帖子

8

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1936
发表于 2020-10-5 09:52:54 | 显示全部楼层
更改&nbsp 只要有暗坑&nbsp 长PTR 选项显式服务提供商;当长=&时,n\u的公共常量覆盖了压缩;H2sData
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2024-11-22 01:24 , Processed in 0.218933 second(s), 60 queries .

© 2020-2024 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表