here is my question:
I have developed a program that uses Microsoft.Excel COM components in order to read/write over Excel files. Well, my app is doing good but when I open, for instance, another file directly with Excel while my program is running, the file(s) that my app uses appear within Excel. I do not want this. I tried also the Visibility property of Excel Application class, but that was not the solution, it just does not work.
NOTE : I have checked this question out.
Restrict access to excel file opened by C# program
Yet, it says no proper solution actually.
You can use
Application.IgnoreRemoteRequests = true. This will avoid users opening excel files in the sameExcelprocess as the one you are using.There is one caveat though: you have to make sure that all execution paths of your application reset this property to
false. This property WILL NOT reset itself when you quit and release yourExcelapplication which means thatExcelwill not respond correctly to a subsequent user who double clicks on a *.xls file for example.EDIT: Possible issues with IgnoreRemoteRequest
Ok, to make this clearer I’ll detail a little bit more what issues you can run into using this feature (at least these are the only ones I’ve run into when I had to use this feature).
When setting
IgnoreRemoteRequests = trueyou have to make sure you reset this property BEFORE quiting and/or releasing the COM Excel application. If you don’t, Excel will not respond to DDE requests which means if someone double clicks on a *.xls file, the file will not open (Excel will start up, but it wont open the file automatically).This however is only true if you quit the application and/or release it without reseting the property. You just have to make sure that wherever it is in your code that you are quitting/resetting you set the
IgnoreRemoteRquestsback tofalsebefore.If you’r application crashes and it hasn’t been able to clean up (unhandled exception) then the EXCEL process will keep running (if invisible, you will only see it in the Task Manager). That is normal as your app didnt have a chance to quit and release the internal Excel it is using. This however is not an issue. If a user ignores this “leaked” Excel process until it’s eventually killed in next reboot or whatever, or manually kills it from the task bar, Excel will work perfectly fine.
Note: MS Excel 2007. Don’t know about behavior of previous versions.