I need to get file name without extension name by VBA. I know ActiveWorkbook.Name
property , but if user haves Windows property Hide extensions for known file types
turn off, the result of my code will be [Name.Extension]. How can I return only name of Workbook independent of windows property?
I try even ActiveWorkbook.Application.Caption
but I can't customize this property.
GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.
Insert filename in cell without extension by Excel formula Select a blank cell, and enter the formula =TRIM(LEFT(SUBSTITUTE(MID(CELL("filename",A1),FIND("[",CELL("filename",A1))+1,255),".
Use VBA DIR function when you want to get the name of the file or a folder, using their path name. To give you an example, if you have an Excel file in a folder, you can use the VBA DIR function to get the name of that Excel file (or any other type of file).
The answers given here already may work in limited situations, but are certainly not the best way to go about it. Don't reinvent the wheel. The File System Object in the Microsoft Scripting Runtime library already has a method to do exactly this. It's called GetBaseName. It handles periods in the file name as is.
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
Simple but works well for me
FileName = ActiveWorkbook.Name If InStr(FileName, ".") > 0 Then FileName = Left(FileName, InStr(FileName, ".") - 1) End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With