|
-
Dec 8th, 2005, 03:40 PM
#1
Thread Starter
Addicted Member
[RESOLVED] API call not working
This is code from John Walkenbach's book "Excel 2000 Power Programming with VBA":
VB Code:
Option Explicit
'32-bit API declarations
Declare Function SHGetPathFromIDList Lib "shell32.dll" _
Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal _
pszpath As String) As Long
Declare Function SHBrowseForFolder Lib "shell32.dll" _
Alias "SHBrowseForFolderA" (lpBrowseInfo As BrowseInfo) _
As Long
Public Type BrowseInfo
hOwner As Long
pIDLRoot As Long
pszDisplayName As String
lpszTitle As String
ulFlags As Long
lpfn As Long
lParam As Long
iImage As Long
End Type
Function GetDirectory(Optional msg) As String
Dim bInfo As BrowseInfo
Dim path As String
Dim r As Long, x As Long, pos As Integer
'Root folder = Desktop
bInfo.pIDLRoot = 0&
'Title in the dialog
If IsMissing(msg) Then
bInfo.lpszTitle = "Please select the folder where 'Old Bio-Analytical MS.xls' exists."
Else
bInfo.lpszTitle = msg
End If
'Type of directory to return
bInfo.ulFlags = &H1
'Display the dialog
x = SHBrowseForFolder(bInfo)
'Parse the result
path = Space$(512)
r = SHGetPathFromIDList(ByVal x, ByVal path)
If r Then
pos = InStr(path, Chr$(10))
[COLOR=Red]GetDirectory = Left(path, pos - 1)[/COLOR]
Else
GetDirectory = ""
End If
End Function
Sub GetAFolder()
Dim msg As String
msg = "Please select a location for the backup."
MsgBox GetDirectory(msg)
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????
-
Dec 9th, 2005, 09:05 AM
#2
Frenzied Member
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
-
Dec 9th, 2005, 11:29 AM
#3
Thread Starter
Addicted Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|