Search code examples
c#winformsexcelvstooffice-interop

How to open Excel file with Read Only protection?


I have opened Excel file in my C# WinForm Application adding reference to Microsoft.Office.Interop.Excel.dll and using DSO FRAMER CONTROL. But I want to open my Excel file with read only protection. I have successfully done this for WORD Application like this:

Word.Document wordDoc = (Word.Document)axFramerControl1.ActiveDocument;
Word.Application wordApp = wordDoc.Application;
wordDoc.Protect(Word.WdProtectionType.wdAllowOnlyReading);

In the same manner, I want to do this work for Excel, but I couldn't protect the Excel file that way.

string path = "C:\\test-wb.xlsx";
axFramerControl1.Open(path, true,"excel.sheet", "", "");

Excel._Workbook excelDoc   =(Microsoft.Office.Interop.Excel._Workbook)axFramerControl1.ActiveDocument;
Excel.Application excelApp =excelDoc.Application;
// What code should I write to protect Excel Workbook with read-only ?
excelDoc.Protect(misval, true, misval);// It is not working.

Solution

  • Call theOpen method with third parameter (ReadOnly) = true.

    See MSDN documentation :

    ReadOnly
    Optional Object. True to open the workbook in read-only mode.