简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM