View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming,microsoft.public.office.developer.vba
Karl E. Peterson Karl E. Peterson is offline
external usenet poster
 
Posts: 22
Default Excel VBA: Browse For Folder - Cancel Button

Steve Rindsberg wrote:
After this line:

filepath = SelectFolder("Please Choose the location of the .txt
files")

Add this:

Dim x As Long
Dim sTemp As String
For x = 1 To Len(filepath)
If Mid$(filepath, x, 1) < Chr$(0) Then
sTemp = sTemp & Mid$(filepath, x, 1)
End If
Next


I'd suggest, instead, that the original SelectFolder function be corrected, so this
isn't necessary...

pidList = SHBrowseForFolder(pBInfo)

If pidList < 0 Then
SHGetPathFromIDList pidList, sPath
CoTaskMemFree pidList
nPos = InStr(sPath, Chr(0))
If nPos 0 Then
sPath = Left(sPath, nPos - 1)
End If
End If

SelectFolder = sPath


There already is TrimNull functionality there, but it only happens *if* the function
succeeds. If the user cancels, or the function fails for any other reason, the full
buffer of vbNullChar's is returned. This could be easily fixed by altering that
algorithm like this:

pidList = SHBrowseForFolder(pBInfo)
If pidList Then
SHGetPathFromIDList pidList, sPath
CoTaskMemFree pidList
nPos = InStr(sPath, Chr(0))
If nPos 0 Then
sPath = Left(sPath, nPos - 1)
End If
Else
sPath = ""
End If
SelectFolder = sPath

' let's see what we got:
If Len(sTemp) 0 Then
Debug.Print sTemp
Else
Debug.Print "User canceled"
exit sub
End If

' Then resume with the rest of the code


Still need that, either way.

Later... Karl
--
Working Without a .NET?
http://classicvb.org/petition