[GuanRui]How to open Path browse dialog in VBA of ArcGIS Desktop ?
2007-08-16 16:50
639 查看
there are 2 ways to open a path browse dialog in VB/VBA. and there is a little diffenent between VB and VBA.
check this out ~
Way No.1 : Legacy VB
1. Add a new module in you VB project .
2. Copy & Paste the code below:
Const BIF_RETURNONLYFSDIRS = &H1
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
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 Function GetDir(ByVal szCaption As String) As String
Dim bi As BROWSEINFO, idl As ITEMIDLIST, rtn As Long, pidl As Long, pos As Long, path As String
bi.hOwner = Me.hwnd (Change Me.hwnd to Application.hwnd when you currently use VBA)
bi.lpszTitle = szCaption
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space(512)
rtn = SHGetPathFromIDList(ByVal pidl, path)
If rtn Then pos = InStr(path$, Chr$(0)): GetDir = Left(path, pos - 1)
End Function
3.In your form, call GetDir(Application.hWnd, "Select a path")
Way No.2 : VB for Application
1. In the VB6 IDE pull down the Project menu and select References. In the
resulting dialog box scroll down to "Microsoft Shell Controls and Automation"
and check the box, then click [OK]. (This is "Adding a Reference".)
2.
Now (after declaring a couple variables, and finding in the VisualC++ help that
the constant BIF_RETURNONLYFSDIRS is 1) this code (similar to Peter's) does in
fact return JUST the folder name. For "I:\Program Files\Microsoft Visual
Studio\VB98\Wizards" it returns the string "Wizards". Not too useful.
3.
Now, thanks to Simon Conway-Smith's post, above, we add the code to retrieve the
full (drive and) path from shlFolder.Items.Item.Path.
Here's the code, in
the on-click method for a button in a test program that actually works (I just
now wrote it).
Private Sub GoButton_Click()
Dim shlShell As Variant, shlFolder As Variant
Set shlShell = New Shell32.Shell
Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Folder", 1)
FolderLabel.Caption = shlFolder.Title
PathLabel.Caption = shlFolder.Items.Item.Path
End Sub
End.
check this out ~
Way No.1 : Legacy VB
1. Add a new module in you VB project .
2. Copy & Paste the code below:
Const BIF_RETURNONLYFSDIRS = &H1
Private Type SHITEMID
cb As Long
abID As Byte
End Type
Private Type ITEMIDLIST
mkid As SHITEMID
End Type
Private Type BROWSEINFO
hOwner As Long
pidlRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
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 Function GetDir(ByVal szCaption As String) As String
Dim bi As BROWSEINFO, idl As ITEMIDLIST, rtn As Long, pidl As Long, pos As Long, path As String
bi.hOwner = Me.hwnd (Change Me.hwnd to Application.hwnd when you currently use VBA)
bi.lpszTitle = szCaption
bi.ulFlags = BIF_RETURNONLYFSDIRS
pidl = SHBrowseForFolder(bi)
path = Space(512)
rtn = SHGetPathFromIDList(ByVal pidl, path)
If rtn Then pos = InStr(path$, Chr$(0)): GetDir = Left(path, pos - 1)
End Function
3.In your form, call GetDir(Application.hWnd, "Select a path")
Way No.2 : VB for Application
1. In the VB6 IDE pull down the Project menu and select References. In the
resulting dialog box scroll down to "Microsoft Shell Controls and Automation"
and check the box, then click [OK]. (This is "Adding a Reference".)
2.
Now (after declaring a couple variables, and finding in the VisualC++ help that
the constant BIF_RETURNONLYFSDIRS is 1) this code (similar to Peter's) does in
fact return JUST the folder name. For "I:\Program Files\Microsoft Visual
Studio\VB98\Wizards" it returns the string "Wizards". Not too useful.
3.
Now, thanks to Simon Conway-Smith's post, above, we add the code to retrieve the
full (drive and) path from shlFolder.Items.Item.Path.
Here's the code, in
the on-click method for a button in a test program that actually works (I just
now wrote it).
Private Sub GoButton_Click()
Dim shlShell As Variant, shlFolder As Variant
Set shlShell = New Shell32.Shell
Set shlFolder = shlShell.BrowseForFolder(Me.hWnd, "Select a Folder", 1)
FolderLabel.Caption = shlFolder.Title
PathLabel.Caption = shlFolder.Items.Item.Path
End Sub
End.
相关文章推荐
- How to open the dialog out of iframe by using jquery dialog
- How to collect the full path name of the files in ASM diskgroups (文档 ID 888943.1)
- How to set number of threads in OpenMP?
- [转]how to programatically access built-in properties of open xml word doc(如何读取open xml格式文档属性)
- How to add,edit and delete key-value in localStorage of Browse
- How to collect the full path name of the files in ASM diskgroups (文档 ID 888943.1)
- How to open UITextView web links in a UIWebView instead of Safari?
- How to keep Z-order of a control in MFC dialog?
- How to Customize Common Open File Dialog Box Places Bar in Windows
- How to make our assemblies appeared in Add Reference dialog box
- iOS 后台定位审核被拒How to clarify the purpose of its use in the locatio
- How to boot ubuntu in text mode instead of graphical(X) mode
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.
- How to get HTML code of a WebElement in Selenium
- How to use the pointer of function in a class?
- How to get the HTML source of a webpage in Ruby
- How Do I Convert an Integer to a String in Excel VBA
- How to make a resizable dialog bar in Microsoft Foundation Classes (MFC)
- [RxJS] How To get the results of two HTTP requests made in sequence
- How to find second highest or maximum salary of Employee in SQL - Interview question