2

In other words, would I need to do some string processing after invoking the Application.GetOpenFileName() Method?

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
stanigator
  • 10,768
  • 34
  • 94
  • 129

6 Answers6

16

Why reinvent the wheel and write tons of boilerplate code? Just use the existing FileSystemObject's GetFileName method, already written and tested and debugged for you:

filename = FSO.GetFileName(path)

Here's a working example:

Dim path As String
Dim filename As String
Dim FSO As Scripting.FileSystemObject
Set FSO = New FileSystemObject

path = "C:\mydir\myotherdir\myfile.txt"

filename = FSO.GetFileName(path) 'Bingo. Done.

Debug.Print filename ' returns "myfile.txt"

' Other features:
Debug.Print FSO.GetBaseName(path) ' myfile
Debug.Print FSO.GetExtensionName(path) ' txt
Debug.Print FSO.GetParentFolderName(path) ' C:\mydir\myotherdir
Debug.Print FSO.GetDriveName(path) ' C:
' et cetera, et cetera.

You will need to set a reference as follows: Tools > References... > set checkmark next to Microsoft Scripting Runtime.

Otherwise use late binding:

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
  • Good point. Didn't know about FileSystemObject class. Thanks for pointing that out. – stanigator Feb 01 '12 at 19:00
  • Yeah, why add a 2-line function to parse a string when you can import an entire class, and use OOP... to parse a string... – Henrik Erlandsson Nov 20 '13 at 14:34
  • 1
    Because it's is already written and already debugged, and there is no downside to using it (unless someone can give me an actual, specific example). You make it sound like OOP is mysterious, but really using the `FileSystemObject` is pretty standard stuff. – Jean-François Corbett Nov 21 '13 at 07:51
5

I am using these functions for filename processing. The last one is the one you need here.

Public Function FilePathOf(ByVal s As String) As String
    Dim pos As Integer

    pos = InStrRev(s, "\")
    If pos = 0 Then
        FilePathOf = ""
    Else
        FilePathOf = Left$(s, pos)
    End If
End Function

Public Function FileNameOf(ByVal s As String) As String
    Dim pos1 As Integer, pos2 As Integer

    pos1 = InStrRev(s, "\") + 1
    pos2 = InStrRev(s, ".")
    If pos2 = Len(s) Then pos2 = pos2 + 1
    If pos2 = 0 Then pos2 = Len(s) + 1
    FileNameOf = Mid$(s, pos1, pos2 - pos1)
End Function

Public Function FileExtOf(ByVal s As String) As String
    Dim pos As Integer

    pos = InStrRev(s, ".")
    If pos = 0 Then
        FileExtOf = ""
    Else
        FileExtOf = Mid$(s, pos + 1)
    End If
End Function

Public Function FileNameExtOf(ByVal s As String) As String
    FileNameExtOf = Mid$(s, InStrRev(s, "\") + 1)
End Function
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • 1
    +1 although I prefer to use the Split function with "\" as the delimiter. – JimmyPena Jan 31 '12 at 21:24
  • 2
    Why reinvent the wheel and write tons of boilerplate code? Just use the existing FileSystemObject... – Jean-François Corbett Feb 01 '12 at 07:53
  • 1
    FileSystemObject is in an external dll. This can cause problems, if the version on your development environment is not the same as on the customers environment if you have a reference to it. You can use late binding so, however you cannot enjoy intellisense any more. – Olivier Jacot-Descombes Feb 01 '12 at 13:22
  • 1
    I guess, in principle -- but in practice? For something so standard as `FileSystemObject`? I have never seen this happen, or heard of it happening, ever. – Jean-François Corbett Nov 21 '13 at 07:50
1

activate the file in question then:

Function getname()

arr = Split(ActiveDocument.FullName, "\")
Debug.Print arr(UBound(arr))

End Function

I assume you are using Word, hence the "ActiveDocument". Change this to "ActiveWorksheet" et al where appropriate

Rich Tier
  • 9,021
  • 10
  • 48
  • 71
0

'Simpler is Always better!! (substitute applicable cell location R1C1, and string length of path)

Dim TheFile As String  
Dim TheFileLessPath As String  

Function getname()  
Workbooks.Open filename:=TheFile  
TheFileLessPath = Mid(TheFile, 12, 7)

ActiveCell.FormulaR1C1 = TheFileLessPath
End Function
0

In this case, you are using Application.GetOpenFilename(), so you are sure that file physically exists on disk, so the simplest approach will be to use Dir().

fileName = Dir(filePath)

Full code is:

Dim fileName, filePath As Variant

filePath = Application.GetOpenFilename("Excel files (*.xlsm), *.xlsm", , "Select desired file", , False)

If filePath = False Then
    MsgBox "No file selected.", vbExclamation, "Sorry!"
    Exit Sub
Else

    'Remove path from full filename
    fileName = Dir(filePath)

    'Print file name (with extension)
    MsgBox "File selected." & vbCr & vbCr & fileName, vbInformation, "Sucess!"

End If
ePandit
  • 2,905
  • 2
  • 24
  • 15
0
Dim myFile As String  
myFile = Application.GetOpenFilename("All Files (*.*), *.*")

'assume myfile is c:\my documents\abc.txt                                         
Debug.Print Dir(myFile) 'this function will return abc.txt
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Benny Law
  • 21
  • 2