а также ей не нравится FnPtr(AddressOf BrowseCallbackProc) в чем дело, кто подскажет.
Код нашел где-то здесь на форуме.
- Код: Выделить всё
Option Explicit
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" ( _
ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Private Enum BrowseFlagsEnum
bifBrowseForComputer = &H1000&
bifBrowseForPrinter = &H2000&
bifBrowseIncludeFiles = &H4000&
bifBrowseIncludeURLs = &H80&
bifShareable = &H8000&
bifDontGoBelowDomain = &H2&
bifEditBox = &H10&
bifReturnFSAncestors = &H8&
bifReturnOnlyFSDirs = &H1&
bifStatusText = &H4&
bifUseNewUI = &H40&
bifValidate = &H20&
End Enum
Private Enum BrowseMessages
'Messages from browser
bffmInitialized = 1
bffmSelChanged = 2
bffmValidateFailedA = 3 'lParam:szPath ret:1(cont),0(EndDialog)
bffmValidateFailedW = 4 'lParam:szPath ret:1(cont),0(EndDialog)
'Messages to browser
bffmSetStatusTextA = (WM_USER + 100)
bffmEnableOk = (WM_USER + 101)
bffmSetSelectionA = (WM_USER + 102)
bffmSetSelectionW = (WM_USER + 103)
bffmSetStatusTextW = (WM_USER + 104)
#If Win32 Then
bffmSetStatusText = bffmSetStatusTextW
bffmSetSelection = bffmSetSelectionW
bffmValidateFailed = bffmValidateFailedW
#Else
bffmSetStatusText = bffmSetStatusTextA
bffmSetSelection = bffmSetSelectionA
bffmValidateFailed = bffmValidateFailedA
#End If
End Enum
Private Type BrowseInfo
hWndOwner As Long
pIDLRoot As Long
pszDisplayName As Long
lpszTitle As Long
ulFlags As BrowseFlagsEnum
lpfnCallback As Long
lParam As Long
iImage As Long
End Type
Private BrowsePath As String
Private Declare Function SHBrowseForFolder Lib "shell32" (lpBI As BrowseInfo) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Function FnPtr(ByVal lp As Long) As Long
FnPtr = lp
End Function
Private Sub RemoveNullChar(ByRef Text As String)
Dim I As Long
I = InStr(Text, vbNullChar)
If I > 0 Then Text = Left$(Text, I - 1)
End Sub
Public Function BrowseCallbackProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long
Select Case uMsg
Case BrowseMessages.bffmInitialized
SendMessage hWnd, BrowseMessages.bffmSetSelectionA, True, ByVal BrowsePath
End Select
BrowseCallbackProc = 0&
End Function
Public Function dlgBrowseFolder(OwnerForm As Form, ByRef Path As String, Optional ByVal Title As String, Optional ByVal AllowExtStyle As Boolean = False, Optional ByVal AllowManualEnter As Boolean = False) As Boolean
Dim BI As BrowseInfo, lpIDList As Long
Const MaxPath As Long = 1024&
BrowsePath = Path
Title = StrConv(Title, vbFromUnicode)
With BI
If OwnerForm Is Nothing Then
.hWndOwner = 0&
Else
.hWndOwner = OwnerForm.hWnd
End If
.pIDLRoot = 0&
.pszDisplayName = 0&
.lpszTitle = StrPtr(Title)
.ulFlags = bifReturnOnlyFSDirs Or IIf(AllowExtStyle, bifUseNewUI, 0&) Or IIf(AllowManualEnter, bifEditBox, 0&)
.lpfnCallback = FnPtr(AddressOf BrowseCallbackProc)
.lParam = 0&
.iImage = 0&
End With
lpIDList = SHBrowseForFolder(BI)
If lpIDList <> 0 Then
Path = String$(MaxPath, 0)
SHGetPathFromIDList lpIDList, Path
CoTaskMemFree lpIDList
RemoveNullChar Path
dlgBrowseFolder = True
End If
End Function
Использование (в форме)
Dim S As String
S = "C:\MyFolder"
If dlgBrowseFolder(Me, S, "Select folder") Then
MsgBox S
End If