Results 1 to 3 of 3

Thread: [RESOLVED] API call not working

  1. #1

    Thread Starter
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Resolved [RESOLVED] API call not working

    This is code from John Walkenbach's book "Excel 2000 Power Programming with VBA":
    VB Code:
    1. Option Explicit
    2.  
    3. '32-bit API declarations
    4. Declare Function SHGetPathFromIDList Lib "shell32.dll" _
    5.     Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
    6.     pszpath As String) As Long
    7.  
    8. Declare Function SHBrowseForFolder Lib "shell32.dll" _
    9.     Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) _
    10.     As Long
    11.  
    12. Public Type BrowseInfo
    13.     hOwner As Long
    14.     pIDLRoot As Long
    15.     pszDisplayName As String
    16.     lpszTitle As String
    17.     ulFlags As Long
    18.     lpfn As Long
    19.     lParam As Long
    20.     iImage As Long
    21. End Type
    22.  
    23. Function GetDirectory(Optional msg) As String
    24.     Dim bInfo As BrowseInfo
    25.     Dim path As String
    26.     Dim r As Long, x As Long, pos As Integer
    27.    
    28.     'Root folder = Desktop
    29.     bInfo.pIDLRoot = 0&
    30.    
    31.     'Title in the dialog
    32.     If IsMissing(msg) Then
    33.         bInfo.lpszTitle = "Please select the folder where 'Old Bio-Analytical MS.xls' exists."
    34.     Else
    35.         bInfo.lpszTitle = msg
    36.     End If
    37.    
    38.     'Type of directory to return
    39.     bInfo.ulFlags = &H1
    40.    
    41.     'Display the dialog
    42.     x = SHBrowseForFolder(bInfo)
    43.    
    44.     'Parse the result
    45.     path = Space$(512)
    46.     r = SHGetPathFromIDList(ByVal x, ByVal path)
    47.     If r Then
    48.         pos = InStr(path, Chr$(10))
    49.         [COLOR=Red]GetDirectory = Left(path, pos - 1)[/COLOR]
    50.     Else
    51.         GetDirectory = ""
    52.     End If
    53. End Function
    54.  
    55. Sub GetAFolder()
    56.     Dim msg As String
    57.     msg = "Please select a location for the backup."
    58.     MsgBox GetDirectory(msg)
    59. End Sub
    The Sub at the bottom is just a test for the call...which isn't working

    I get the error where the red text is. Apparently variable pos = 0

    Any ideas????




    If you find any of my posts of good help, please rate it

  2. #2
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: API call not working

    I added some comments here:
    Code:
        'Parse the result
    'This establishes a buffer for the returned string
        path = Space$(512)
    'Fetch the string
        r = SHGetPathFromIDList(ByVal x, ByVal path)
        If r Then
    'Find an ASCII Line Feed in the string ???
            pos = InStr(path, Chr$(10))
    'The Directory is the string up to, but not including the Line Feed
            GetDirectory = Left(path, pos - 1)
        Else
            GetDirectory = ""
        End If
    If there is no Line Feed in the returned string, the result of the "pos" line is zero (0), and the next line will try to find the left part of the string starting at position -1, which is invalid and will give you the error message. You'll just have to find out the actual contents of the string so that you can parse it. Since there are obviously non-printing characters embedded in the string, you might insert a test loop to walk through the string character by characters showing you the codes for each character. Then you can figure out how to parse it. Maybe the line separator is not actually "10" (Line Feed), but perhaps "13" Carriage Return? (Just a guess).
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  3. #3

    Thread Starter
    Addicted Member malik641's Avatar
    Join Date
    Sep 2005
    Location
    South Florida :-)
    Posts
    221

    Re: API call not working

    Thanks for the info Webtest. I actually looped from character 1 to 255 to find the right one and I couldn't

    I had this post on another website and it was solved there.

    And apparently Windows stores strings with a null to flag the end of the string. Char(0) is that null.

    So Char(0) it is!




    If you find any of my posts of good help, please rate it

Posting Permissions

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



Click Here to Expand Forum to Full Width