Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File name without extension name VBA

Tags:

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.

like image 678
Liniel Avatar asked Jan 13 '15 14:01

Liniel


People also ask

How do you get the name of a file without the extension?

GetFileNameWithoutExtension(ReadOnlySpan<Char>) Returns the file name without the extension of a file path that is represented by a read-only character span.

How do I name a file without an extension in Excel?

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

How do you define file names in VBA?

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


2 Answers

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

like image 156
RubberDuck Avatar answered Sep 20 '22 12:09

RubberDuck


Simple but works well for me

FileName = ActiveWorkbook.Name  If InStr(FileName, ".") > 0 Then     FileName = Left(FileName, InStr(FileName, ".") - 1)  End If 
like image 37
Ifca Avatar answered Sep 21 '22 12:09

Ifca