我需要通过VBA获取没有扩展名的文件名。我知道ActiveWorkbook.Name
属性,但如果用户有Windows属性Hide extensions for known file types
关闭,我的代码的结果将是[Name.Extension]。如何只返回与Windows属性无关的工作簿名称?
我甚至尝试ActiveWorkbook.Application.Caption
,但我无法自定义此属性。
答案 0 :(得分:56)
这里给出的答案可能在有限的情况下有效,但肯定不是最好的方法。不要重新发明轮子。 File System Object中的Microsoft Scripting Runtime library已经有了完成此操作的方法。它被称为GetBaseName。它按原样处理文件名中的句点。
Public Sub Test()
Dim fso As New Scripting.FileSystemObject
Debug.Print fso.GetBaseName(ActiveWorkbook.Name)
End Sub
Public Sub Test2()
Dim fso As New Scripting.FileSystemObject
Debug.Print fso.GetBaseName("MyFile.something.txt")
End Sub
Instructions for adding a reference to the Scripting Library
答案 1 :(得分:6)
简单但对我有用
FileName = ActiveWorkbook.Name
If InStr(FileName, ".") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
答案 2 :(得分:0)
为了详细说明,展示了对扩展的删除 工作簿..现在有各种扩展。 。一个新的未保存的Book1没有ext 。文件
的工作方式相同[代码]
函数WorkbookIsOpen(FWNa $,可选AnyExt As Boolean = False)作为布尔值
Dim wWB As Workbook, WBNa$, PD%
FWNa = Trim(FWNa)
If FWNa <> "" Then
For Each wWB In Workbooks
WBNa = wWB.Name
If AnyExt Then
PD = InStr(WBNa, ".")
If PD > 0 Then WBNa = Left(WBNa, PD - 1)
PD = InStr(FWNa, ".")
If PD > 0 Then FWNa = Left(FWNa, PD - 1)
'
' the alternative of using split.. see commented out below
' looks neater but takes a bit longer then the pair of instr and left
' VBA does about 800,000 of these small splits/sec
' and about 20,000,000 Instr Lefts per sec
' of course if not checking for other extensions they do not matter
' and to any reasonable program
' THIS DISCUSSIONOF TIME TAKEN DOES NOT MATTER
' IN doing about doing 2000 of this routine per sec
' WBNa = Split(WBNa, ".")(0)
'FWNa = Split(FWNa, ".")(0)
End If
If WBNa = FWNa Then
WorkbookIsOpen = True
Exit Function
End If
Next wWB
End If
结束功能 [/代码]
答案 3 :(得分:0)
在我看来,使用Split函数似乎比InStr和Left更优雅。
Private Sub CommandButton2_Click()
Dim ThisFileName As String
Dim BaseFileName As String
Dim FileNameArray() As String
ThisFileName = ThisWorkbook.Name
FileNameArray = Split(ThisFileName, ".")
BaseFileName = FileNameArray(0)
MsgBox "Base file name is " & BaseFileName
End Sub
答案 4 :(得分:0)
这将从最后一个字符开始获取文件类型(因此避免了文件名中出现点的问题)
Function getFileType(fn As String) As String
''get last instance of "." (full stop) in a filename then returns the part of the filename starting at that dot to the end
Dim strIndex As Integer
Dim x As Integer
Dim myChar As String
strIndex = Len(fn)
For x = 1 To Len(fn)
myChar = Mid(fn, strIndex, 1)
If myChar = "." Then
Exit For
End If
strIndex = strIndex - 1
Next x
getFileType = UCase(Mid(fn, strIndex, Len(fn) - x + 1))
结束功能
答案 5 :(得分:0)
我使用来自我的personal.xlsb 的宏并在xlsm 和xlsx 文件上运行它,因此我使用的David Metcalfe 答案的一个变体是
<块引用>Dim Wrkbook As String
<块引用>Wrkbook = Replace(Application.ActiveWorkbook.Name, ".xlsx", ".pdf")
<块引用>Wrkbook = Replace(Application.ActiveWorkbook.Name, ".xlsm", ".pdf")
答案 6 :(得分:-1)
您总是可以使用Replace()
,因为您是在工作簿的名称上执行此操作的,而使用VBA几乎可以肯定以.xlsm
结尾。
根据您的示例使用ActiveWorkbook:
Replace(Application.ActiveWorkbook.Name, ".xlsm", "")
使用本工作簿:
Replace(Application.ThisWorkbook.Name, ".xlsm", "")
答案 7 :(得分:-2)
答案在这里: 我觉得这个答案很好,请试一试 http://mariaevert.dk/vba/?p=162
答案 8 :(得分:-4)
strTestString = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))