Rui Martins
Rui Martins

Reputation: 2184

Getting the name of a excel file

Let me first tell that it's the first time I'm doing this.

I'm developing a add-in in c# for excel where a user is suposed to open a file and then press a start button in the add-in to start the all process.

Now, what I want to know is how to get the name of the open file. I don't know where the file is located, so I i can't use FileInfo.

The file name is of this type "Cxxx_Curr_REC_surplus.xls".

I've been trying, but all I get is the name of the active workbook.

I'd like to know if this is possible.

Upvotes: 3

Views: 10227

Answers (1)

Kash
Kash

Reputation: 9019

Try this:

Microsoft.Office.Interop.Excel.Application myExcel;
this.Activate ( );
myExcel = ( Microsoft.Office.Interop.Excel.Application ) System.Runtime.InteropServices.Marshal.GetActiveObject ( "Excel.Application" )
MessageBox.Show ( myExcel.ActiveWorkbook.FullName ); // gives full path
MessageBox.Show ( myExcel.ActiveWorkbook.Name ); // gives file name

Reference:
How to use Visual C# to automate a running instance of an Office program
MSDN

Upvotes: 6

Related Questions