Hide file path on a text box

G

Guest

How can I improve this code?... I want to select a file on the computer, show
the name in a textbox after selected, and then clicking a button to open it.

So far this is what I have, is not exactly what I want but it works...
what I want to fix right now, is to hide the entire path of the file to be
opened and only display the file name... how can I do that?

fileToOpen = Application.GetOpenFilename("Excel files (*.xls), *.xls")
If fileToOpen <> False Then TextBox1.Text = fileToOpen
 
M

Mika

From J. Walkenbach:

Private Function FileNameOnly(pname) As String
' Returns the filename from a path/filename string
Dim i As Integer, length As Integer, temp As String
length = Len(pname)
temp = ""
For i = length To 1 Step -1
If Mid(pname, i, 1) = Application.PathSeparator Then
FileNameOnly = temp
Exit Function
End If
temp = Mid(pname, i, 1) & temp
Next i
FileNameOnly = pname
End Function

Rgds
Mika
 

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