The GetDirectory Function - Is there anyone familiar with it
Results 1 to 2 of 2

Thread: The GetDirectory Function - Is there anyone familiar with it
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Apr 2002
    Posts
    26
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default The GetDirectory Function - Is there anyone familiar with it

    I came across the code for the GetDirectory Funtion and it was just the thing I needed. I applied the code but when it is being executed I get the error message - "runtime error 13, type mismatch. It then highlights the line

    r = SHGetPathFromIDList (ByVal x, ByVal path)

    Can anyone help or do you need to see the worksheet .

    Slims

  2. #2
    MrExcel MVP
    Join Date
    Feb 2002
    Location
    Bogota, Colombia
    Posts
    11,959
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    That is an API function, which does this:
    Converts an item identifier list to a file system path.
    AllApi.net has this example:

    Code:
    Private Type BrowseInfo
        hWndOwner As Long
        pIDLRoot As Long
        pszDisplayName As Long
        lpszTitle As Long
        ulFlags As Long
        lpfnCallback As Long
        lParam As Long
        iImage As Long
    End Type
    Const BIF_RETURNONLYFSDIRS = 1
    Const MAX_PATH = 260
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    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 Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'KPDTeam@Allapi.net
        Dim iNull As Integer, lpIDList As Long, lResult As Long
        Dim sPath As String, udtBI As BrowseInfo
    
        With udtBI
            'Set the owner window
            .hWndOwner = Me.hWnd
            'lstrcat appends the two strings and returns the memory address
            .lpszTitle = lstrcat("C:\", "")
            'Return only if the user selected a directory
            .ulFlags = BIF_RETURNONLYFSDIRS
        End With
    
        'Show the 'Browse for folder' dialog
        lpIDList = SHBrowseForFolder(udtBI)
        If lpIDList Then
            sPath = String$(MAX_PATH, 0)
            'Get the path from the IDList
            SHGetPathFromIDList lpIDList, sPath
            'free the block of memory
            CoTaskMemFree lpIDList
            iNull = InStr(sPath, vbNullChar)
            If iNull Then
                sPath = Left$(sPath, iNull - 1)
            End If
        End If
    
        MsgBox sPath
    End Sub
    Change Me.hWnd to 0. You may want to check Stephen Bullen's site for a more robust way to handle this functions, enabling you to specify the starting directory (www.bmsltd.co.uk)

    And also, this code is to paste directly into a VB Form, but you can use it in VBA by changing the sub name (Private Sub Form_Load) to something like

    Sub ShowDir()
    Regards,

    Juan Pablo González
    http://www.juanpg.com

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •