001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103 |
|
Option Explicit
Private Declare PtrSafe Function MoveWindow Lib "user32.dll" ( _
ByVal hwnd As LongPtr, ByVal x As Long, _
ByVal y As Long, ByVal nWidth As Long, _
ByVal nHeight As Long, ByVal bRepaint As Long) As LongPtr
Private Declare PtrSafe Function GetSystemMetrics Lib "user32.dll" ( _
ByVal nIndex As Long) As Long
Private Declare PtrSafe Function GetWindowRect Lib "user32.dll" ( _
ByVal hwnd As LongPtr, ByRef lpRect As RECT) As Long
Private Declare PtrSafe Function CoTaskMemFree Lib "ole32" ( _
ByVal hMem As LongPtr) As LongPtr
Private Declare PtrSafe Function SHGetPathFromIDList Lib "shell32" ( _
ByVal pList As LongPtr, ByVal lpBuffer As String) As LongPtr
Private Declare PtrSafe Function SendMessageA Lib "user32.dll" ( _
ByVal hwnd As LongPtr, ByVal Msg As Long, _
wParam As LongPtr, lParam As Any) As LongPtr
Private Declare PtrSafe Function SHBrowseForFolderA Lib "Shell32.dll" ( _
lpBrowseInfo As InfoT) As LongPtr
Private Type InfoT
hwnd As LongPtr
Root As LongPtr
DisplayName As String
Title As String
Flags As Long
FName As LongPtr
lParam As LongPtr
Image As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private s_BrowseInitDir As String
Private Function BrowseCallback( _
ByVal hwnd As LongPtr, ByVal uMsg As Long, _
ByVal wParam As LongPtr, ByVal lParam As LongPtr) As Long
If uMsg = &H1 Then
Call SendMessageA(hwnd, &H466, ByVal 1&, ByVal s_BrowseInitDir)
Call CenterDialog(hwnd)
End If
BrowseCallback = 0
End Function
Private Function FuncCallback(ByVal nParam As LongPtr) As LongPtr
FuncCallback = nParam
End Function
Private Sub CenterDialog(ByVal hwnd As LongPtr)
Dim WinRect As RECT, ScrWidth As Long, ScrHeight As Long
Dim DlgWidth As Integer, DlgHeight As Integer
GetWindowRect hwnd, WinRect
DlgWidth = WinRect.Right - WinRect.Left
DlgHeight = WinRect.Bottom - WinRect.Top
ScrWidth = GetSystemMetrics(&H10)
ScrHeight = GetSystemMetrics(&H11)
MoveWindow hwnd, (ScrWidth - DlgWidth) / 2, _
(ScrHeight - DlgHeight) / 2, DlgWidth, DlgHeight, 1
End Sub
Public Function fncGetFolder( _
Optional ByVal sMsg As String = "Bitte wählen Sie ein Verzeichnis", _
Optional ByVal sPath As String = "C:\") As String
Dim xl As InfoT, IDList As LongPtr, RVal As LongPtr, FolderName As String
If sPath Like "*.???" Or sPath Like "*.????" Then
sPath = Left$(sPath, InStrRev(sPath, "\"))
End If
If Dir(sPath, vbDirectory) = "" Then
sPath = ThisWorkbook.Path
End If
s_BrowseInitDir = sPath
With xl
.hwnd = Application.hwnd
.Title = sMsg ' lstrcat(sMsg, "")
.Flags = &H1
.FName = FuncCallback(AddressOf BrowseCallback)
End With
IDList = SHBrowseForFolderA(xl)
If IDList <> 0 Then
FolderName = Space(256)
RVal = SHGetPathFromIDList(IDList, FolderName)
CoTaskMemFree (IDList)
FolderName = Trim$(FolderName)
FolderName = Left$(FolderName, Len(FolderName) - 1)
End If
fncGetFolder = FolderName
End Function
Sub TestKHV()
MsgBox fncGetFolder()
End Sub
|