Return file name only from FileDialog

P

PMC1

Hi,

I'm looking for a way to return only the File Name from the FileDialog
object. From what I can see I can only return both the full path and
file name.

I'm open to suggestion of alternative methods other than FileDialog
such as an API but again any API examples I have found only return the
full path.

Has anybody any suggestions on how i could do this?

Thanks

Paul
 
G

Guest

Most would strip out the filename from the fullpathName

sFileName =
Right(sFullPathName,Len(sFullPathBName)-InstrRev(sFullPathName,"\"))

demo'd from the immediate window:

sFullPathName = "C:\MyMainsubFolder\MySecondarySubFolder\Myfile.xls"
? sFullPathname
C:\MyMainsubFolder\MySecondarySubFolder\Myfile.xls
sFileName =
Right(sFullPathName,Len(sFullPathName)-InstrRev(sFullPathName,"\"))
? sfileName
Myfile.xls
 
M

moon

If you import GetOpenFileName (comdlg32.dll), the string to return should be
the FileTitle, not the FileName...

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias
"GetOpenFileNameA" (pFILETOOPEN As FILETOOPEN) As Long

Private Type FILETOOPEN
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Private Sub GetFileName()
Dim FTO As FILETOOPEN
FTO.lStructSize = Len(FTO)
FTO.hwndOwner = 0
FTO.hInstance = 0
FTO.lpstrFilter = "Text Files (*.txt)" + Chr$(0) + "*.txt" + Chr$(0) +
"All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
FTO.lpstrFile = Space$(254)
FTO.nMaxFile = 255
FTO.lpstrFileTitle = Space$(254)
FTO.nMaxFileTitle = 255
FTO.lpstrInitialDir = "C:\"
FTO.lpstrTitle = "Select a File..."
FTO.flags = 0
If GetOpenFileName(FTO) Then
MsgBox "FileName: " + Trim$(FTO.lpstrFileTitle)
Else
'Do nothing
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top