2

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.

2 Answers 2

14

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

See MSDN documentation :

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

Sign up to request clarification or add additional context in comments.

3 Comments

:Thanks gdoron.Can you please tell where should i give that parameter in my above code that i mentioned in my question?
@Saravanan. According to the MSDN, you should open with third param true. which .Net framework do you use? .Net 4?
This helped me. Instead however I just added the password as the 6th parameter of the Open method.
-1

The WorkBook class has a Protect method, similar (but not identical) to the one supported by Word. I can't find the COM/interop documentation, but the VSTO documentation covers the same ground, and the method signatures are the same:

Protect

Protects a workbook so that it cannot be modified.

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

(This all assumes that what you wanted to achieve was to protect the document, since that's what the Word code does, as opposed to opening the document read only, which is what your narrative seems to be saying, in which case, @gdoron's answer is more suitable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.