【问题标题】:How do you get just the filename rather than the entire file path of an open file?您如何仅获取文件名而不是打开文件的整个文件路径?
【发布时间】:2012-02-23 13:43:04
【问题描述】:

也就是说,在调用Application.GetOpenFileName()方法之后,我需要做一些字符串处理吗?

【问题讨论】:

  • 你的意思是Excel的Application.GetOpenFilename方法吗?

标签: excel vba filenames getopenfilename


【解决方案1】:

为什么要重新发明轮子并编写大量样板代码?只需使用现有的FileSystemObject 的 GetFileName 方法,已经为您编写、测试和调试过:

filename = FSO.GetFileName(path)

这是一个工作示例:

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.

您需要按如下方式设置参考: 工具 > 参考... > 在 Microsoft Scripting Runtime 旁边设置复选标记。

否则使用后期绑定:

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemObject")

【讨论】:

  • 好点。不知道 FileSystemObject 类。感谢您指出这一点。
  • 是啊,既然可以导入整个类,为什么还要加两行函数来解析字符串,并使用OOP...解析字符串...
  • 因为它已经编写好并且已经调试过了,并且使用它没有缺点(除非有人可以给我一个实际的、具体的例子)。你让 OOP 听起来很神秘,但实际上使用 FileSystemObject 是非常标准的东西。
【解决方案2】:

我将这些函数用于文件名处理。最后一个就是你需要的。

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

【讨论】:

  • +1 虽然我更喜欢使用“\”作为分隔符的拆分功能。
  • 为什么要重新发明轮子并编写大量样板代码?只需使用现有的 FileSystemObject...
  • FileSystemObject 位于外部 dll 中。如果您的开发环境中的版本与客户环境中的版本不同(如果您有参考),这可能会导致问题。你可以使用后期绑定,但是你不能再享受智能感知了。
  • 我想,原则上——但实际上呢?对于像FileSystemObject 这样标准的东西?我从未见过这种情况发生,也从未听说过。
【解决方案3】:

然后激活相关文件:

Function getname()

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

End Function

我假设您使用的是 Word,因此是“ActiveDocument”。在适当的情况下将其更改为“ActiveWorksheet”等

【讨论】:

    【解决方案4】:

    '越简单越好!! (替换适用的单元格位置R1C1,以及路径的字符串长度)

    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】:

      在这种情况下,您使用的是 Application.GetOpenFilename(),因此您确定文件物理上存在于磁盘上,因此最简单的方法是使用 Dir()。

      fileName = Dir(filePath)
      

      完整代码为:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-29
        • 2013-03-21
        相关资源
        最近更新 更多