Results 1 to 18 of 18

Thread: help on browse dialog

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    help on browse dialog

    hi, i've a browse dialog with the following constants....however i want to get the filename so does anyone know how i can get that.

    VB Code:
    1. Public Const BIF_RETURNONLYFSDIRS = &H1         'Only returns file system directories
    2. Public Const BIF_DONTGOBELOWDOMAIN = &H2        'Does not include network folders below the domain level
    3. Public Const BIF_STATUSTEXT = &H4               'Includes a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
    4. Public Const BIF_RETURNFSANCESTORS = &H8        'Only returns file system ancestors
    5. Public Const BIF_BROWSEFORCOMPUTER = &H1000     'Only returns computers
    6. Public Const BIF_BROWSEFORPRINTER = &H2000      'Only returns (network) printers
    7. Public Const BIF_BROWSEINCLUDEFILES As Long = &H4000
    8.  
    9. Public Const MAX_PATH = 255

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    I am not sure exactly what you are doing. Do you mean the Browse for File dialog? Why not use the Common Dialog control to do that?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: help on browse dialog

    i'm trying to get the effect of "open file" of microsoft word. I already have the code and everything is functional & i can retrieve the filepath but, I just need to get the fileName. Here's the code I'm using:

    VB Code:
    1. Public Function SelectFolder(frm As Form, _Optional sDialTitle As String = "Select a folder") As String
    2.  
    3.     On Error Resume Next
    4.     Dim bi As BROWSEINFO
    5.     Dim pidl As Long
    6.     Dim Path As String
    7.     Dim Pos As Integer
    8.  
    9.         'Fill the BROWSEINFO structure with the needed data.
    10.         With bi
    11.  
    12.             .hOwner = frm.hwnd
    13.             .pidlRoot = 0&                      'Root folder to browse from, or desktop if Null
    14.             .lpszTitle = sDialTitle$             'Message to display in dialog
    15.             .ulFlags = BIF_RETURNONLYFSDIRS     'the type of folder to return
    16.  
    17.         End With
    18.  
    19.         'show the browse for folders dialog
    20.         pidl = SHBrowseForFolder(bi)
    21.  
    22.         'the dialog has closed, so parse & display the user's
    23.         'returned folder selection contained in pidl
    24.         Path = Space$(MAX_PATH)
    25.  
    26.         If SHGetPathFromIDList(ByVal pidl, ByVal Path) Then
    27.             Pos = InStr(Path, Chr$(0))
    28.             SelectFolder = Left$(Path, Pos - 1)
    29.             If StrComp(Mid$(SelectFolder$, Len(SelectFolder$)), "\", vbTextCompare) <> 0 Then SelectFolder$ = SelectFolder$ & "\"
    30.         Else
    31.             SelectFolder$ = ""
    32.         End If
    33.  
    34.         Call CoTaskMemFree(pidl)
    35.  
    36. End Function


    and here are the structs used in it

    VB Code:
    1. '***************************************************************
    2. '   Opens a common dialog window to browse for a folder
    3. '   Returns the path to the folder selected as a string
    4. '***************************************************************
    5.  
    6. '***************************************************************
    7. '   Browse Dialog Constants
    8. '***************************************************************
    9. Public Type BROWSEINFO
    10.     hOwner           As Long         'Handle to window's owner
    11.     pidlRoot         As Long         'Pointer to an item identifier list
    12.     pszDisplayName   As String       'Pointer to a buffer that receives the display name of the folder selected
    13.     lpszTitle        As String       'Pointer to a null-terminated string that is displayed above the tree view control in the dialog box
    14.     ulFlags          As Long         'Value specifying the types of folders to be listed in the dialog box as well as other options
    15.     lpfn             As Long         'Address an application-defined function that the dialog box calls when events occur
    16.     lParam           As Long         'Application-defined value that the dialog box passes to the callback function (if one is specified).
    17.     iImage           As Long         'Variable that receives the image associated with the selected folder. The image is specified as an index to the system image list.
    18. End Type
    19.  
    20. '***************************************************************
    21. '   Browse Dialog Flags & Constants
    22. '***************************************************************
    23. Public Const BIF_RETURNONLYFSDIRS = &H1         'Only returns file system directories
    24. Public Const BIF_DONTGOBELOWDOMAIN = &H2        'Does not include network folders below the domain level
    25. Public Const BIF_STATUSTEXT = &H4               'Includes a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
    26. Public Const BIF_RETURNFSANCESTORS = &H8        'Only returns file system ancestors
    27. Public Const BIF_BROWSEFORCOMPUTER = &H1000     'Only returns computers
    28. Public Const BIF_BROWSEFORPRINTER = &H2000      'Only returns (network) printers
    29.  
    30. Public Const MAX_PATH = 255

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    this is how you can extract the filename out of the full path:

    strFileName = Right$(strPath, Len(strPath) - InStrRev(strPath, "\"))

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: help on browse dialog

    no that didnt' work, it didnt' give me the fileName still

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    Can you get the path? I mean path including the filename.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: help on browse dialog

    Which API are you using? You are only checking for system folders, not file names.

  8. #8
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    Also, have a look at this control from the CCRP project: http://ccrp.mvps.org/download/controls/ccrpbd10.zip

  9. #9
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    Sorry about that, wrong control. This is the correct link: http://ccrp.mvps.org/download/controls/ccrpfd6.zip

  10. #10

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: help on browse dialog

    no i can get the whole path like "C:\Documents and Settings\Owner\My Documents\ShareWorks\Filespree\Current Beta Client\client.txt" but i just want "client.txt" in the variable sPath that i get from this line:

    sPath$ = SelectFile(Me, "Select file")

    i'm sorry i copied the wrong code, here's the one for selectFile function (there's a difference of one flag only)

    VB Code:
    1. Public Function SelectFile(frm As Form, _
    2.                              Optional sDialTitle As String = "Select a file") As String
    3.  
    4.     On Error Resume Next
    5.     Dim bi As BROWSEINFO
    6.     Dim pidl As Long
    7.     Dim Path As String
    8.     Dim Pos As Integer
    9.  
    10.         'Fill the BROWSEINFO structure with the needed data.
    11.         With bi
    12.  
    13.             .hOwner = frm.hwnd
    14.             .pidlRoot = 0&                      'Root folder to browse from, or desktop if Null
    15.             .lpszTitle = sDialTitle$             'Message to display in dialog
    16.             .ulFlags = BIF_BROWSEINCLUDEFILES    'the type of folder to return
    17.  
    18.         End With
    19.  
    20.         'show the browse for folders dialog
    21.         pidl = SHBrowseForFolder(bi)
    22.  
    23.         'the dialog has closed, so parse & display the user's
    24.         'returned folder selection contained in pidl
    25.         Path = Space$(MAX_PATH)
    26.  
    27.         If SHGetPathFromIDList(ByVal pidl, ByVal Path) Then
    28.             Pos = InStr(Path, Chr$(0))
    29.             SelectFile = Left$(Path, Pos - 1)
    30.             If StrComp(Mid$(SelectFile$, Len(SelectFile$)), "\", vbTextCompare) <> 0 Then SelectFile$ = SelectFile$ & "\"
    31.         Else
    32.             SelectFile$ = ""
    33.         End If
    34.  
    35.         Call CoTaskMemFree(pidl)
    36.  
    37. End Function

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: help on browse dialog

    I fixed it up and its working now.
    VB Code:
    1. 'In Module1.bas
    2. Option Explicit
    3.  
    4. '***************************************************************
    5. '   Opens a common dialog window to browse for a folder
    6. '   Returns the path to the folder selected as a string
    7. '***************************************************************
    8.  
    9. '***************************************************************
    10. '   Browse Dialog Constants
    11. '***************************************************************
    12. Public Type BROWSEINFO
    13.     hOwner           As Long         'Handle to window's owner
    14.     pidlRoot         As Long         'Pointer to an item identifier list
    15.     pszDisplayName   As Long       'Pointer to a buffer that receives the display name of the folder selected
    16.     lpszTitle        As Long       'Pointer to a null-terminated string that is displayed above the tree view control in the dialog box
    17.     ulFlags          As Long         'Value specifying the types of folders to be listed in the dialog box as well as other options
    18.     lpfn             As Long         'Address an application-defined function that the dialog box calls when events occur
    19.     lParam           As Long         'Application-defined value that the dialog box passes to the callback function (if one is specified).
    20.     iImage           As Long         'Variable that receives the image associated with the selected folder. The image is specified as an index to the system image list.
    21. End Type
    22.  
    23. '***************************************************************
    24. '   Browse Dialog Flags & Constants
    25. '***************************************************************
    26. Public Const BIF_RETURNONLYFSDIRS = &H1         'Only returns file system directories
    27. Public Const BIF_DONTGOBELOWDOMAIN = &H2        'Does not include network folders below the domain level
    28. Public Const BIF_STATUSTEXT = &H4               'Includes a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box.
    29. Public Const BIF_RETURNFSANCESTORS = &H8        'Only returns file system ancestors
    30. Public Const BIF_BROWSEFORCOMPUTER = &H1000     'Only returns computers
    31. Public Const BIF_BROWSEFORPRINTER = &H2000      'Only returns (network) printers
    32. Public Const BIF_BROWSEINCLUDEFILES As Long = &H4000
    33.  
    34. Public Const MAX_PATH = 255
    35.  
    36. Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    37. Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
    38. Private Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BROWSEINFO) As Long
    39. Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (ByRef pidl As Long, ByVal pszPath As String) As Long
    40.  
    41. Public Function SelectFolder(ByRef frm As Form, Optional sDialTitle As String = "Select a folder") As String
    42.  
    43.     On Error Resume Next
    44.     Dim bi As BROWSEINFO
    45.     Dim sPath As String
    46.     Dim lPos As Long
    47.  
    48.         'Fill the BROWSEINFO structure with the needed data.
    49.         With bi
    50.             .hOwner = frm.hWnd
    51.             .pidlRoot = 0&                      'Root folder to browse from, or desktop if Null
    52.             .lpszTitle = lstrcat(sDialTitle, "") 'Message to display in dialog
    53.             .ulFlags = BIF_BROWSEINCLUDEFILES     'the type of folder to return
    54.         End With
    55.         'show the browse for folders dialog
    56.         lPos = SHBrowseForFolder(bi)
    57.         'the dialog has closed, so parse & display the user's
    58.         'returned folder selection contained in pidl
    59.         sPath = Space$(MAX_PATH)
    60.         If lPos Then
    61.             SHGetPathFromIDList ByVal lPos, ByVal sPath
    62.             CoTaskMemFree lPos
    63.             lPos = InStr(sPath, vbNullChar)
    64.             sPath = Left$(sPath, lPos - 1)
    65.             SelectFolder = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
    66.         Else
    67.             SelectFolder = ""
    68.         End If
    69.  
    70. End Function
    71.  
    72.  
    73. 'Behind Form1.frm
    74. Option Explicit
    75.  
    76. Private Sub Command1_Click()
    77.     Dim sFile As String
    78.     sFile = SelectFolder(Me, "Select File")
    79.     MsgBox sFile
    80. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    After you get the full path, just use this code to extract the file from it:

    strFileName = Right$(strPath, Len(strPath) - InStrRev(strPath, "\"))

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: help on browse dialog

    I borrowed your file parsing code and alread have it in there.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: help on browse dialog

    Oh, I didn't see that post. I didnt refresh before replying.

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: help on browse dialog

    I was debating on if I should just parse on the return of the filepath or parse in the function. It would be best to parse on the
    return so you have the option of which one you need at any particular time.


    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: help on browse dialog

    thnxs for ur input but, that's wht i'm trying to figure out too..cuz i want to parse outside cuz i need both the path and file...but i can't call this line:
    CoTaskMemFree lPos
    outside the module cuz i'm calling selectFile function elsewhere in a different form, or maybe i can :S...actually its not my code so i'm not quite sure what these getPath and browseforFolder functions are doing and how it works (i'm new to VB too

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: help on browse dialog

    Then why not just return the complete file path because its easy to parse outsire the function.
    VB Code:
    1. 'Change this line from:
    2. SelectFolder = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
    3.  
    4. 'To
    5. SelectFolder = Left$(sPath, lPos - 1)
    6.  
    7.  
    8. 'Then parse in your call in your form if you need just the filename:
    9. SelectFolder = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  18. #18

    Thread Starter
    Lively Member
    Join Date
    May 2005
    Posts
    90

    Re: help on browse dialog

    yep that's how i did it i have this code to get the filename

    VB Code:
    1. Pos = InStrRev(Filename, "\")
    2.     If Pos > 0 Then
    3.         RemovePath = Right$(Filename, Len(Filename) - Pos)
    4.     End If

    thanks a lot for your time and help

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