By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 979 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Get the path of a file hosted by an excel process in C#

P: 3
Hi,
I have a method in C# which checks if any Excel process is running on my system. If it finds any process is there a way to get the directory path of the file running in that excel process through C# code ? For example Book1.xls is running on my machine,C# code identifies this process and i need to get the directory path where Book1.xls is stored. I need this code ASAP.

Thanks.
Dec 11 '08 #1
Share this Question
Share on Google+
4 Replies


nukefusion
Expert 100+
P: 221
You could certainly get the filename using interop and the office type library if a single instance of Excel is open. You'd use Marshal.GetActiveObject to get the instance of excel and then call ActiveWorkbook.FullName on the instance to retrieve the workbook filename.

If there is more than one instance open you might have trouble getting hold of the exact instance you want in order to extract the file name. Quite a lot of those issues have been covered recently in another thread here: Can't get correct instance of Excel for paste
Dec 11 '08 #2

P: 3
In my case there might be more than one excel file open. So i am checking for each process in the multiple excel processes. Can i get the path of each file by having a FOR EACH Loop. If there is a way can you plz show me the sample code
Dec 11 '08 #3

nukefusion
Expert 100+
P: 221
If you can get the path for each Excel instances active workbook then I'm afraid I don't know how to do it.
These problems have already been covered in the thread I posted the link for. It may be worthwhile reading that thread as it also includes code samples that may give you an idea of how to proceed.
Dec 11 '08 #4

P: 3
Thankyou so much for your help.

On the other hand is there any way in C# where i can find whether a file in a folder is already open? If the file is already open then on click of a button on windows form it should activate the already opened file. If the file is not open the button click should open the file from the specifiec folder. Is there a way to do this in C#.Net. Any sample code is highly appreciated.

Thanks.
Dec 11 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.