How to get the file name from a dialogue box selection?

PuddinPie

Registered User.
Local time
Today, 12:38
Joined
Sep 15, 2010
Messages
149
I'm tying to get just the file name that get's selected in a dialogue box. I can get the path and the path and the file name but not just the file name.

Here what I have so far:

'Set up the File Dialog.
Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
With fDialog
'Allow user to make multiple selections in dialog box.
.AllowMultiSelect = False
'Set the title of the dialog box.
.Title = "Please select a scan model that this can be modeled after."
.Filters.Add "All Files", "*.*"
If .Show = True Then

oldname = fDialog.SelectedItems.item(1)
NewName = "\\Llfsp2\IRISEUC\RTM\2007\RTMSorageFolder\ScanModels\"
retval = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
retval = objFSO.CopyFile(oldname, NewName, True)
Else
Exit Function
End If
End With
 
from what I was reading this morning when looking into file names and dialouge boxes in seperat contexts this morning it should be possible to count the length into the string of the last "\" then cut the string from the charcter following that? will post some code in a min

Code:
Dim x as long, char as string, CharCount as long
 
char = "\"
 
For x = 1 To Len(oldname)
 
If Mid(oldname, x, Len(Char)) = Char Then
ChrCount = ChrCount +1 
End If
next
 
oldname = Right(oldname, Len(oldname) - charcount)

hope that helps
 
Last edited:
It's still giving me the whole path plus the file name.
 
that's only a quick bit of sudocode not sure how valid it is when put into use
 
Ok. It's been figured out.

Function getFileName(MyFileName As String)
' --- Pulls the File name and extension from a full path
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(MyFileName) Then
getFileName = fso.GetBaseName(MyFileName) & "." & fso.GetExtensionName(MyFileName)
End If
Set fso = Nothing
End Function
 
that's a much neater solution in all fairness
 

Users who are viewing this thread

Back
Top Bottom