VBA Get File Name

Solved
pampiou Posted messages 24 Status Membre -  
 gogodu75 -
Hello programming experts!

I'm currently working on a program in Visual Basic with Excel. I would like to automatically retrieve the file name and store it in a variable so as to avoid changing all the lines of code where the file name is referenced.
So far, I've done this:
Dim name As String
name = Workbooks.Application.Name

But the problem is that the variable "name" returns "Microsoft Excel".

If any of you have a solution to retrieve the file name (without the path), I'm all ears!!!

Thanks in advance for your help.

1 réponse

pijaku Posted messages 13513 Registration date   Status Modérateur Last intervention   2 771
 
Hello,
If you don't need the file extension:
Dim FileName As String FileName = ThisWorkbook.Name MsgBox FileName
If you need the extension:
Dim FileName As String FileName = ThisWorkbook.Name & ".xls" 'or ".xlsm" etc... MsgBox FileName

--
Franck P
12
pampiou Posted messages 24 Status Membre 2
 
Thank you very much!!!
There wasn't much to find, but after a good hour of searching without finding anything, you saved me :p
1
gogodu75
 
Thank you, beach bro!!!
1