Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 i want to do this work for Excel.But i couldn't able to protect Excel file on 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.
share|improve this question

2 Answers 2

up vote 7 down vote accepted

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

See MSDN documentation :

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

share|improve this answer
    
:Thanks gdoron.Can you please tell where should i give that parameter in my above code that i mentioned in my question? –  Saravanan Jan 18 '12 at 9:26
    
@Saravanan. According to the MSDN, you should open with third param true. which .Net framework do you use? .Net 4? –  gdoron Jan 18 '12 at 11:27
    
:yes i am using .net 4 –  Saravanan Jan 18 '12 at 11:30
    
This helped me. Instead however I just added the password as the 6th parameter of the Open method. –  Lukas Apr 28 at 15:56

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

share|improve this answer
    
I used excelDoc.Protect(misval, true, misval);But it is not working... –  Saravanan Jan 18 '12 at 9:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.