简体   繁体   中英

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

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.

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 normally stop any documents/workbooks being opened via your instance when the user opens via shortcut.

But sometimes this fails (eg if there are left over instances of Word in processes).

To make my instance of Word "exclusive" I am currently doing the following (until something better comes along):

  1. Opening a dummy document as explained above.
  2. Disable all functionality within Word which allows for opening of documents.
  3. Just in case 1 fails try and catch any documents opened by my instance of Word and close them.

The last part of the process is kludgy, but I can't see any other way around it.

Originally, I used DocumentOpen and DocumentNew events to catch document opening and new documents and this works well. You can close the documents in these events and reopen them without apparent issue.

However, further testing showed that DocumentOpen and DocumentNew events only fire if the document is opened using your Word instance ie if the user uses the backstage for example. If they click on a Word shortcut, even though the document opens in your instance, it does not (why ever not MS?) fire the above events.

The only way I can find to capture Word documents opened by whatever means is to use the DocumentChange event and loop through each document and if it isn't the one I want, close it and reopen it.

Unfortunately, this does not work in a straightforward manner. It seems to work okay for new documents, but if the user opens an existing document and you attempt to close it in the DocumentChange event, it causes your principal document to hang (at least it does for me).

To get around this I use the DocumentChange event to find the offending docs and fire off a timer (100 ms seems to do the trick) to handle the actual closing and opening.

All in all utterly horrible and I wish someone will come back and tell me that I have wasted days on the above because Office applications have a "MakeMyInstanceUnique" property which does exactly what I need!

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