[英]File name without extension name VBA
我需要通過 VBA 獲取沒有擴展名的文件名。 我知道ActiveWorkbook.Name
屬性,但如果用戶關閉了 Windows 屬性Hide extensions for known file types
,我的代碼的結果將是 [Name.Extension]。 如何僅返回獨立於 Windows 屬性的工作簿名稱?
我什至嘗試ActiveWorkbook.Application.Caption
但我無法自定義此屬性。
這里給出的答案可能在有限的情況下有效,但肯定不是最好的方法。 不要重新發明輪子。 Microsoft Scripting Runtime 庫中的 文件系統對象已經有一種方法可以做到這一點。 它被稱為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
簡單但對我很有效
FileName = ActiveWorkbook.Name
If InStr(FileName, ".") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
在我看來,使用 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
這會從最后一個字符獲取文件類型(因此避免了文件名中的點問題)
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))
結束功能
詳細地說,為工作簿演示了刪除擴展名。現在有各種擴展名。 . 新的未保存 Book1 沒有分機。 文件的工作方式相同
Function WorkbookIsOpen(FWNa$, Optional AnyExt As Boolean = False) As Boolean
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
End Function
您始終可以使用Replace()
,因為您是在工作簿的名稱上執行此操作,由於使用 VBA,它幾乎肯定會以.xlsm
結尾。
根據您的示例使用 ActiveWorkbook:
Replace(Application.ActiveWorkbook.Name, ".xlsm", "")
使用本工作簿:
Replace(Application.ThisWorkbook.Name, ".xlsm", "")
我使用個人.xlsb 中的宏並在 xlsm 和 xlsx 文件上運行它,因此我使用的 David Metcalfe 答案的變體是
將 Wrkbook 變暗為字符串
Wrkbook = Replace(Application.ActiveWorkbook.Name, ".xlsx", ".pdf")
Wrkbook = Replace(Application.ActiveWorkbook.Name, ".xlsm", ".pdf")
如果您不想使用 FSO,這是一個解決方案。 之前有一些類似的答案,但這里進行了一些檢查以處理名稱和名稱中的多個點而無需擴展名。
Function getFileNameWithoutExtension(FullFileName As String)
Dim a() As String
Dim ext_len As Integer, name_len As Integer
If InStr(FullFileName, ".") = 0 Then
getFileNameWithoutExtension = FullFileName
Exit Function
End If
a = Split(ActiveWorkbook.Name, ".")
ext_len = Len(a(UBound(a))) 'extension length (last element of array)
name_len = Len(FullFileName) - ext_len - 1 'length of name without extension and a dot before it
getFileNameWithoutExtension = Left(FullFileName, name_len)
End Function
Sub test1() 'testing the function
MsgBox (getFileNameWithoutExtension("test.xls.xlsx")) ' -> test.xls
MsgBox (getFileNameWithoutExtension("test")) ' -> test
MsgBox (getFileNameWithoutExtension("test.xlsx")) ' -> test
End Sub
最近這個話題對我很有幫助。 只是為了擴展@RubberDuck 的答案,Microsoft Scripting Runtime 庫中的文件系統對象已經可以實現這一目標。 此外,如果您將其定義為如下所示的對象,它將為您省去必須在 VBA Tools > References中啟用“Microsoft Scripting Runtime”的麻煩:
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Debug.Print fso.GetBaseName(ActiveWorkbook.Name)
這樣,它將返回沒有擴展名的 ActiveWorkbook 的名稱。
使用 INSTRREV 函數還有另一種方法,如下所示:
Dim fname As String
fname = Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1)
MsgBox fname
兩者都將返回相同的結果。 同樣在上述兩種方法中,它們將保留文件名中的任何句號,並且只刪除最后一個句號和文件擴展名。
strTestString = Left(ThisWorkbook.Name, (InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1))
完整的信用: http ://mariaevert.dk/vba/?p=162
我需要通過VBA獲取不帶擴展名的文件名。 我知道ActiveWorkbook.Name
屬性,但是如果用戶具有Windows屬性,則會關閉“ Hide extensions for known file types
,我的代碼結果將是[Name.Extension]。 如何僅返回獨立於Windows屬性的工作簿名稱?
我什至嘗試使用ActiveWorkbook.Application.Caption
但無法自定義此屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.