How to open excel file with read-only protection?

I opened the Excel file in my C # WinForm application by adding a link to Microsoft.Office.Interop.Excel.dll and using DSO FRAMER CONTROL. But I want to open read-only excel file. I have successfully done this for a WORD application like this

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

In the same I want to make this work for Excel. But I could not protect the Excel file in this 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. 
+1
c # excel winforms vsto office-interop
Jan 18 '12 at 7:33
source share
2 answers

Call the Open method with the third parameter ( ReadOnly ) = true .

See the MSDN documentation :

Readonly
Optional object. True to open a book in read-only mode.

+11
Jan 18 '12 at 7:41
source share

The WorkBook class has a Protect method that is similar (but not identical) to that supported by Word. I cannot find the COM / interop documentation, but the VSTO documentation covers the same foundation, and the method signatures are the same:

Protect

Protects the workbook so that it cannot be changed.

 public virtual void Protect ( [OptionalAttribute] Object Password, [OptionalAttribute] Object Structure, [OptionalAttribute] Object Windows ) 

(All of this assumes that what you wanted to achieve is to protect the document, because this is what the Word code does, as opposed to opening only the document that you think your story tells, @gdoron answer is more suitable

-one
Jan 18 '12 at 8:00
source share



All Articles