Results 1 to 5 of 5

Thread: browse for folder

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    browse for folder

    Greetings,

    A while ago someone gave me this function for browsing for a folder. It works great for me on my machine. The problem is, that when I install the program on our customer's 98 machine, the "Choose a folder box" does not appear when she clicks the button; and then she has to enter the folder name manually.

    Any idea why it acts that way on her machine, and how I can fix the problem?

    The code is below.

    Thank you,
    Jim

    Private Sub cmdDirectoryBrowse_Click()
    Dim i, flag As Integer
    Dim sAnswer(50) As String
    i = 0
    flag = 1
    sAnswer(i) = " " & BrowseForFolder("Choose a folder!", 0, 0)
    lstDirectoriesToScan.AddItem (sAnswer(i))
    While flag = 1
    If MsgBox("Would you like an additional directory to be scanned?", vbYesNo) = vbYes Then
    'Yes was selected
    i = i + 1
    sAnswer(i) = " " & BrowseForFolder("Choose a folder!", 0, 0)
    lstDirectoriesToScan.AddItem (sAnswer(i))
    Else
    'No was selected
    flag = 0
    End If
    Wend
    dircount = lstDirectoriesToScan.ListCount
    End Sub

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    What's the code for BrowseForFolder()?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586

    code for BrowseForFolder

    Function BrowseForFolder(ByVal pstrPrompt, _
    ByVal pintBrowseType, _
    ByVal pintLocation)
    On Error Resume Next

    Dim ShellObject As Object
    Dim pstrTempFolder As Variant
    Dim clngColonPosition As Variant
    Const NO_PARENT = &H0

    Set ShellObject = CreateObject("Shell.Application")

    Set pstrTempFolder = ShellObject.BrowseForFolder(NO_PARENT, pstrPrompt, _
    pintBrowseType, pintLocation)

    BrowseForFolder = pstrTempFolder.ParentFolder.ParseName( _
    pstrTempFolder.Title).PATH

    If Err.Number <> 0 Then
    BrowseForFolder = Null

    '--Handles cancel
    If Err.Number = 424 Then
    Set ShellObject = Nothing
    Set pstrTempFolder = Nothing
    Exit Function
    End If

    '--If the user selects a drive, the drive letter is converted to
    '--a path by adding a colon and backslash to the drive letter.
    clngColonPosition = InStr(1, pstrTempFolder.Title, ":", vbTextCompare)
    If clngColonPosition > 0 Then
    BrowseForFolder = Mid(pstrTempFolder.Title, (clngColonPosition - 1), 2) & "\"
    End If
    End If

    Set ShellObject = Nothing
    Set pstrTempFolder = Nothing

    End Function

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2002
    Posts
    586
    So... any clues why the Browse box doesn't show on our customer's 98 machine; but it shows on ours?

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    The object can probably not be created


    this

    VB Code:
    1. '--Handles cancel
    2. If Err.Number = 424 Then
    3. Set ShellObject = Nothing
    4. Set pstrTempFolder = Nothing
    5. Exit Function
    6. End If

    tells if the object is created or not.... try removing it and run on the 98 computer.

    it it errs, the problem is that there are some files missing....

    also try the api version of browse for folder

    VB Code:
    1. Option Explicit
    2.  
    3. Public Type BrowseInfo
    4.      hwndOwner As Long
    5.      pIDLRoot As Long
    6.      pszDisplayName As Long
    7.      lpszTitle As Long
    8.      ulFlags As Long
    9.      lpfnCallback As Long
    10.      lParam As Long
    11.      iImage As Long
    12. End Type
    13.  
    14. Public Const BIF_RETURNONLYFSDIRS = 1
    15. Public Const BIF_NEWDIALOGSTYLE = &H40
    16.  
    17. Public Const MAX_PATH = 260
    18. Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    19. Public Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    20. Public Declare Function SHBrowseForFolder Lib "shell32" (lpbi As BrowseInfo) As Long
    21. Public Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList As Long, ByVal lpBuffer As String) As Long
    22.  
    23. Public Function BrowseForFolder(hwndOwner As Long, sPrompt As String) As String
    24.      
    25.     'declare variables to be used
    26.      Dim iNull As Integer
    27.      Dim lpIDList As Long
    28.      Dim lResult As Long
    29.      Dim sPath As String
    30.      Dim udtBI As BrowseInfo
    31.  
    32.     'initialise variables
    33.      With udtBI
    34.         .hwndOwner = hwndOwner
    35.         .lpszTitle = lstrcat(sPrompt, "")
    36.         .ulFlags = BIF_RETURNONLYFSDIRS + BIF_NEWDIALOGSTYLE
    37.      End With
    38.  
    39.     'Call the browse for folder API
    40.      lpIDList = SHBrowseForFolder(udtBI)
    41.      
    42.     'get the resulting string path
    43.      If lpIDList Then
    44.         sPath = String$(MAX_PATH, 0)
    45.         lResult = SHGetPathFromIDList(lpIDList, sPath)
    46.         Call CoTaskMemFree(lpIDList)
    47.         iNull = InStr(sPath, vbNullChar)
    48.         If iNull Then sPath = Left$(sPath, iNull - 1)
    49.      End If
    50.  
    51.     'If cancel was pressed, sPath = ""
    52.      BrowseForFolder = sPath
    53.  
    54. End Function
    55.  
    56.  
    57. 'usage :
    58. Private Sub Form_Click()
    59.     MsgBox BrowseForFolder(Me.hWnd, "select folder")
    60. End Sub

    think that should work...
    -= a peet post =-

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