Private Sub Command1_Click() Me.Caption = BrowseFolder("vælg en mappe.", "C:\") End Sub '-------------------------------------------- Form1 --------------------------------------------
Private Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BROWSEINFO) As Long Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (pidl As Any, ByVal pszPath As String) As Long Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long Private 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 Type BROWSEINFO hwndOwner As Long pidlRoot As Long sDisplayName As String sTitle As String ulFlags As Long lpfn As Long lParam As Long iImage As Long End Type
Private Const WM_USER As Long = &H400& Private Const MAX_PATH As Long = 260& Private Const BFFM_INITIALIZED As Long = 1& Private Const BFFM_SETSELECTION As Long = (WM_USER + 102&) Private Const BIF_RETURNONLYFSDIRS As Long = &H1&
Private m_strPath As String
Private Function GetCBaddr(ByVal lAddr As Long) As Long GetCBaddr = lAddr End Function
Private Function BFCallBack(ByVal hWnd As Long, ByVal uMsg As Long, ByVal lParam As Long, ByVal lpData As Long) As Long If uMsg = BFFM_INITIALIZED Then SendMessage hWnd, BFFM_SETSELECTION, 1&, ByVal m_strPath End If End Function
Public Function BrowseFolder(Optional ByVal strPrompt As String = "", Optional ByVal strPath As String = "") As String Dim BINF As BROWSEINFO Dim strDir As String Dim iLen As Integer Dim lpItem As Long
m_strPath = strPath If m_strPath <> "" Then iLen = Len(m_strPath) If iLen >= 2 Then If Right(m_strPath, 1) = "\" Then If iLen > 3 Then m_strPath = Left$(m_strPath, iLen - 1) Else If iLen < 3 Then m_strPath = m_strPath & "\" End If On Error GoTo EH_SBOA If Dir(m_strPath, vbDirectory) <> "" Then BINF.lpfn = GetCBaddr(AddressOf BFCallBack) End If End If End If
EH_SBOA: On Error GoTo 0
With BINF .hwndOwner = Screen.ActiveForm.hWnd .pidlRoot = 0& .sDisplayName = Space$(MAX_PATH + 1) If Len(strPrompt) > 0 Then .sTitle = strPrompt Else .sTitle = "Select a folder." End If .ulFlags = BIF_RETURNONLYFSDIRS End With
lpItem = SHBrowseForFolder(BINF) If lpItem Then strDir = Space$(MAX_PATH) If SHGetPathFromIDList(ByVal lpItem, strDir) Then BrowseFolder = Left$(strDir, InStr(1, strDir, Chr(0)) - 1) End If End If
Call GlobalFree(lpItem) m_strPath = "" End Function '------------------------------------------- Module1 -------------------------------------------
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.