How do you get just the filename rather than the entire file path of an open file?

孤人 提交于 2019-11-26 22:12:36

问题


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


回答1:


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



回答2:


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")



回答3:


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




回答4:


'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



回答5:


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


来源:https://stackoverflow.com/questions/9086309/how-do-you-get-just-the-filename-rather-than-the-entire-file-path-of-an-open-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!