|
-
Jun 20th, 2005, 08:43 PM
#1
Thread Starter
Lively Member
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:
Public Const BIF_RETURNONLYFSDIRS = &H1 'Only returns file system directories
Public Const BIF_DONTGOBELOWDOMAIN = &H2 'Does not include network folders below the domain level
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.
Public Const BIF_RETURNFSANCESTORS = &H8 'Only returns file system ancestors
Public Const BIF_BROWSEFORCOMPUTER = &H1000 'Only returns computers
Public Const BIF_BROWSEFORPRINTER = &H2000 'Only returns (network) printers
Public Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Public Const MAX_PATH = 255
-
Jun 20th, 2005, 09:00 PM
#2
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?
-
Jun 20th, 2005, 09:06 PM
#3
Thread Starter
Lively Member
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:
Public Function SelectFolder(frm As Form, _Optional sDialTitle As String = "Select a folder") As String
On Error Resume Next
Dim bi As BROWSEINFO
Dim pidl As Long
Dim Path As String
Dim Pos As Integer
'Fill the BROWSEINFO structure with the needed data.
With bi
.hOwner = frm.hwnd
.pidlRoot = 0& 'Root folder to browse from, or desktop if Null
.lpszTitle = sDialTitle$ 'Message to display in dialog
.ulFlags = BIF_RETURNONLYFSDIRS 'the type of folder to return
End With
'show the browse for folders dialog
pidl = SHBrowseForFolder(bi)
'the dialog has closed, so parse & display the user's
'returned folder selection contained in pidl
Path = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal pidl, ByVal Path) Then
Pos = InStr(Path, Chr$(0))
SelectFolder = Left$(Path, Pos - 1)
If StrComp(Mid$(SelectFolder$, Len(SelectFolder$)), "\", vbTextCompare) <> 0 Then SelectFolder$ = SelectFolder$ & "\"
Else
SelectFolder$ = ""
End If
Call CoTaskMemFree(pidl)
End Function
and here are the structs used in it
VB Code:
'***************************************************************
' Opens a common dialog window to browse for a folder
' Returns the path to the folder selected as a string
'***************************************************************
'***************************************************************
' Browse Dialog Constants
'***************************************************************
Public Type BROWSEINFO
hOwner As Long 'Handle to window's owner
pidlRoot As Long 'Pointer to an item identifier list
pszDisplayName As String 'Pointer to a buffer that receives the display name of the folder selected
lpszTitle As String 'Pointer to a null-terminated string that is displayed above the tree view control in the dialog box
ulFlags As Long 'Value specifying the types of folders to be listed in the dialog box as well as other options
lpfn As Long 'Address an application-defined function that the dialog box calls when events occur
lParam As Long 'Application-defined value that the dialog box passes to the callback function (if one is specified).
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.
End Type
'***************************************************************
' Browse Dialog Flags & Constants
'***************************************************************
Public Const BIF_RETURNONLYFSDIRS = &H1 'Only returns file system directories
Public Const BIF_DONTGOBELOWDOMAIN = &H2 'Does not include network folders below the domain level
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.
Public Const BIF_RETURNFSANCESTORS = &H8 'Only returns file system ancestors
Public Const BIF_BROWSEFORCOMPUTER = &H1000 'Only returns computers
Public Const BIF_BROWSEFORPRINTER = &H2000 'Only returns (network) printers
Public Const MAX_PATH = 255
-
Jun 20th, 2005, 09:10 PM
#4
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, "\"))
-
Jun 20th, 2005, 09:28 PM
#5
Thread Starter
Lively Member
Re: help on browse dialog
no that didnt' work, it didnt' give me the fileName still
-
Jun 20th, 2005, 09:34 PM
#6
Re: help on browse dialog
Can you get the path? I mean path including the filename.
-
Jun 20th, 2005, 09:40 PM
#7
Re: help on browse dialog
Which API are you using? You are only checking for system folders, not file names.
-
Jun 20th, 2005, 09:45 PM
#8
Re: help on browse dialog
Also, have a look at this control from the CCRP project: http://ccrp.mvps.org/download/controls/ccrpbd10.zip
-
Jun 20th, 2005, 10:02 PM
#9
Re: help on browse dialog
Sorry about that, wrong control. This is the correct link: http://ccrp.mvps.org/download/controls/ccrpfd6.zip
-
Jun 20th, 2005, 10:18 PM
#10
Thread Starter
Lively Member
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:
Public Function SelectFile(frm As Form, _
Optional sDialTitle As String = "Select a file") As String
On Error Resume Next
Dim bi As BROWSEINFO
Dim pidl As Long
Dim Path As String
Dim Pos As Integer
'Fill the BROWSEINFO structure with the needed data.
With bi
.hOwner = frm.hwnd
.pidlRoot = 0& 'Root folder to browse from, or desktop if Null
.lpszTitle = sDialTitle$ 'Message to display in dialog
.ulFlags = BIF_BROWSEINCLUDEFILES 'the type of folder to return
End With
'show the browse for folders dialog
pidl = SHBrowseForFolder(bi)
'the dialog has closed, so parse & display the user's
'returned folder selection contained in pidl
Path = Space$(MAX_PATH)
If SHGetPathFromIDList(ByVal pidl, ByVal Path) Then
Pos = InStr(Path, Chr$(0))
SelectFile = Left$(Path, Pos - 1)
If StrComp(Mid$(SelectFile$, Len(SelectFile$)), "\", vbTextCompare) <> 0 Then SelectFile$ = SelectFile$ & "\"
Else
SelectFile$ = ""
End If
Call CoTaskMemFree(pidl)
End Function
-
Jun 20th, 2005, 10:21 PM
#11
Re: help on browse dialog
I fixed it up and its working now.
VB Code:
'In Module1.bas
Option Explicit
'***************************************************************
' Opens a common dialog window to browse for a folder
' Returns the path to the folder selected as a string
'***************************************************************
'***************************************************************
' Browse Dialog Constants
'***************************************************************
Public Type BROWSEINFO
hOwner As Long 'Handle to window's owner
pidlRoot As Long 'Pointer to an item identifier list
pszDisplayName As Long 'Pointer to a buffer that receives the display name of the folder selected
lpszTitle As Long 'Pointer to a null-terminated string that is displayed above the tree view control in the dialog box
ulFlags As Long 'Value specifying the types of folders to be listed in the dialog box as well as other options
lpfn As Long 'Address an application-defined function that the dialog box calls when events occur
lParam As Long 'Application-defined value that the dialog box passes to the callback function (if one is specified).
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.
End Type
'***************************************************************
' Browse Dialog Flags & Constants
'***************************************************************
Public Const BIF_RETURNONLYFSDIRS = &H1 'Only returns file system directories
Public Const BIF_DONTGOBELOWDOMAIN = &H2 'Does not include network folders below the domain level
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.
Public Const BIF_RETURNFSANCESTORS = &H8 'Only returns file system ancestors
Public Const BIF_BROWSEFORCOMPUTER = &H1000 'Only returns computers
Public Const BIF_BROWSEFORPRINTER = &H2000 'Only returns (network) printers
Public Const BIF_BROWSEINCLUDEFILES As Long = &H4000
Public Const MAX_PATH = 255
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
Private Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal lpString1 As String, ByVal lpString2 As String) As Long
Private Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BROWSEINFO) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" (ByRef pidl As Long, ByVal pszPath As String) As Long
Public Function SelectFolder(ByRef frm As Form, Optional sDialTitle As String = "Select a folder") As String
On Error Resume Next
Dim bi As BROWSEINFO
Dim sPath As String
Dim lPos As Long
'Fill the BROWSEINFO structure with the needed data.
With bi
.hOwner = frm.hWnd
.pidlRoot = 0& 'Root folder to browse from, or desktop if Null
.lpszTitle = lstrcat(sDialTitle, "") 'Message to display in dialog
.ulFlags = BIF_BROWSEINCLUDEFILES 'the type of folder to return
End With
'show the browse for folders dialog
lPos = SHBrowseForFolder(bi)
'the dialog has closed, so parse & display the user's
'returned folder selection contained in pidl
sPath = Space$(MAX_PATH)
If lPos Then
SHGetPathFromIDList ByVal lPos, ByVal sPath
CoTaskMemFree lPos
lPos = InStr(sPath, vbNullChar)
sPath = Left$(sPath, lPos - 1)
SelectFolder = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
Else
SelectFolder = ""
End If
End Function
'Behind Form1.frm
Option Explicit
Private Sub Command1_Click()
Dim sFile As String
sFile = SelectFolder(Me, "Select File")
MsgBox sFile
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 20th, 2005, 10:24 PM
#12
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, "\"))
-
Jun 20th, 2005, 10:26 PM
#13
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 20th, 2005, 10:31 PM
#14
Re: help on browse dialog
Oh, I didn't see that post. I didnt refresh before replying.
-
Jun 20th, 2005, 10:34 PM
#15
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 20th, 2005, 10:43 PM
#16
Thread Starter
Lively Member
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
-
Jun 20th, 2005, 10:55 PM
#17
Re: help on browse dialog
Then why not just return the complete file path because its easy to parse outsire the function.
VB Code:
'Change this line from:
SelectFolder = Right$(sPath, Len(sPath) - InStrRev(sPath, "\"))
'To
SelectFolder = Left$(sPath, lPos - 1)
'Then parse in your call in your form if you need just the filename:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 20th, 2005, 11:03 PM
#18
Thread Starter
Lively Member
Re: help on browse dialog
yep that's how i did it i have this code to get the filename
VB Code:
Pos = InStrRev(Filename, "\")
If Pos > 0 Then
RemovePath = Right$(Filename, Len(Filename) - Pos)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|