Hiding an open Excel file

here is my question:

I developed a program that uses Microsoft.Excel COM components to read / write Excel files. Well, my application works well, but when I open, for example, another file directly from Excel while my program is running, the files used by my application are displayed in Excel. I do not want this. I also tried the Visibility property of the Excel application class, but this was not a solution, it just didn't work.

NOTE. I checked this question.

Restrict access to excel file opened by C # program

However, he is not talking about the right decision.

+4
source share
2 answers

You can use Application.IgnoreRemoteRequests = true . This will avoid opening excel files in the same Excel process as the one you are using.

However, there is one caveat: you must make sure that all execution paths of your application reset this property to false . This property WILL NOT be reset when exiting and releasing an Excel application, which means that Excel will not correctly respond to the next user who double-clicks the * .xls file, for example.

EDIT: Possible problems with IgnoreRemoteRequest

Well, to make this clearer, I’ll talk a little more about what problems you can use in using this function (at least these are the only ones that I encountered when I had to use this function).

When setting IgnoreRemoteRequests = true you must ensure that you reset this property BEFORE the Excel COM application is missing and / or released. If you do not, Excel will not respond to DDE requests, which means that if someone double-clicks the * .xls file, the file will not open (Excel will start, but it will not open the file automatically).

This, however, is true only if you left the application and / or released it without reselling this property. You just need to make sure that no matter where you are in your code, that you leave / reload, you will set IgnoreRemoteRquests back to false earlier.

If your application crashes and cannot fail (unhandled exception), then the EXCEL process will continue to work (if it is invisible, you will only see it in the task manager). This is normal, since your application did not have the ability to exit and release the internal Excel that it uses. This, however, is not a problem. If the user ignores this β€œleaked” Excel process until it is killed on the next reboot or something else, or manually removes it from the taskbar, Excel will work fine.

Note. MS Excel 2007. I do not know about the behavior of previous versions.

+2
source

Have you tried to run your program under a service account? This should avoid that the excel com object interferes with the instance used by the registered console user, so they will not see the effects of your COM objects. There is probably also a better security practice for running COM applications under the service account, not for the user account, but for another question.

0
source

All Articles