Open excel file in exclusive mode using interop in c #

In my application, I want to be able to open an excel file that already exists and add information to that file. I am using Microsoft Interop libraries. I noticed that if you create a new spreadsheet using Add and then Save, you can set the file in exclusive mode until you close the excel application.

However, I haven't found a way to do the same when opening an existing file. The reason I want to do this is because if I open an excel file while my application is open, I get an exception. My guess is that when I open the excel file with excel, it automatically opens in exclusive mode.

I though open the file and then use saveas but it doesn't work.

private object missing=Type.Missing;
workbook = workbooks.Open(excelfilename,missing, false, missing, missing, missing,missing,missing,missing,missing,false, missing,missing, missing, missing);
workbook.SaveAs((String)excelfilename, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges, missing, missing, missing, missing); 


I get the exception later in code:

Excel.Range testrange = sheet.get_Range(rangetext);


The exception I am getting is COMException, "Exception from HRESULT: 0x800401A8".


source to share

2 answers

Have a look at the ExclusiveAccess property of the Workbook class:



You can go to this article for a complete example on how to open excel file / add new sheet / get range and set properties with Microsoft interop library



All Articles