C# After opening an excel workbook, how can I prevent a user from opening another excel file(via windows) in the same excel instance?


Question [C# After opening an excel workbook, how can I prevent a user from opening another excel file(via windows) in the same excel instance?] has 1 solution 2020-08-19 02:59:33 c#

I have created a program that searches excel, word, and txt files for a user entered string. So I open each file, search for the string, and add the file(with info) to a datagrid if the document contains this string.

The program works great, except for some unforseen situations. If the user did not have excel open when they start the search, the program opens a new instance of excel and begins searching. During this search, if the user then opens an excel file from windows explorer, it will open it in the same instance that my program is using, which then proceeds to show all the files it is opening, searching, then closing.

If the user already has excel open, then my program opens it's own instance and there is no issue. The exact same issue applies to word documents as well.

My question is, how can I prevent the user from opening a file in the same instance of excel that my program is currently using?

Here are the basics of how I am accessing excel:

using Excel = Microsoft.Office.Interop.Excel;

Excel.Application xlsApp = new Excel.Application();
Excel.Workbook wkb = null;
Excel.Workbooks wkbs = xlsApp.Workbooks;


xlsApp.DisplayAlerts = false;
wkb = wkbs.Open(filePath, ReadOnly: true);



//Do search here...


//Close the workbook when necessary...
wkb.Close(false);

//Close the app when necessary...
xlsApp.Quit();

I'm hoping there's some parameter I can set to prevent the user from opening documents in the same instance.


Question [C# After opening an excel workbook, how can I prevent a user from opening another excel file(via windows) in the same excel instance?] solution number 1

Not sure if you are still looking for a solution to this problem after all this time. I have been trying to solve this issue myself and with help from the guys at Add-In Express I have come up with a workable solution. The following should work for you.

Create your Word/Excel instance with ckNewInstance. Open a new document/workbook Close it. Open the document/workbook you intend to work with.

This will stop any documents/workbooks being opened via your instance when the user opens via shortcut. As you don't make the excel/word instance visible you shouldn't have to worry about anything else. NB there's no need to make the instance visible for this to work.

For anyone who needs to make the instance visible there is the added problem that if the user opens a new document/workbook via the Word/Excel instance it will be opened in your instance and not a new one. I am still working on this. I have tried to disable all possible functionality for opening a new document from my instance. I haven't quite got there.

As something of a (hopefully) temporary kludge I am intercepting the onDocumentOpen event grabbing the details of the opened document, closing it and reopening it in its a new instance. Not an ideal solution, but it works.


.htaccess .net .net-core 2d 3d 3d-printing abp abstract-syntax-tree actions-on-google actionscript-3 active-directory activemq activemq-artemis acumatica adobe-xd aframe ag-grid agora.io air airflow ajax akka alert alexa algorithm alignment allure amadeus amazon-cloudformation amazon-cognito