bullpit

Hi all,

I am doing some excel automation from my windows application. From the code, I open an Excel Workbook, write into it, and then close it while saving it. Is it possible to make this file or workbook readonly It should be strictly readonly and nobody should be able to modify it, even by doing a right click and unchecking the Read Only checkbox from the Properties.

This is what I tried but didnt work:

Code Block

m_objBook.ChangeFileAccess(Excel.XlFileAccess.xlReadOnly, Type.Missing, false);

m_objBook.Close(true, savePath, Type.Missing);



Re: Visual Studio Tools for Office Make excel workbook readonly

Ji Zhou – MSFT

Hi,

What about protecting every sheet in the workbook Code looks like:

app.Workbooks.Open(@"C:\test.xlsx", missing, missing, missing, missing, missing,

missing, missing, missing, missing, missing, missing, missing, missing, missing);

int count = app.Worksheets.Count;

for (int i = 1; i <= count;i++ )

{

Excel.Worksheet ws = app.WorksheetsIdea as Excel.Worksheet;

ws.Protect("851230",missing,missing,missing,missing,missing,missing,

missing,missing,missing,missing,missing,missing,missing,missing,

missing);

}

Hope it helps!

Thanks

Ji






Re: Visual Studio Tools for Office Make excel workbook readonly

bullpit

Thanks Ji,

That certainly is a solution and I kinda figured that out while I was playing with my code. Instead of looping thru all the sheets, since I had only three sheets and was working on them at different points and modules, I password protected each of them seperately and it seems to work.

I tried password protecting the whole workbook but that didn't work. Do you know why

Thanks so much...

bullpit





Re: Visual Studio Tools for Office Make excel workbook readonly

Ji Zhou – MSFT

Hi Bullpit,

Protecting workbook only makes users cannot add or remove sheets, but not restricts us to edit the cells. Protecting workbook and protecting worksheet are different concepts.

Please refer the following links:

http://msdn2.microsoft.com/en-us/library/7czw9wzs(VS.80).aspx

http://j-walk.com/ss/excel/faqs/protectionFAQ.htm

Thanks

Ji






Re: Visual Studio Tools for Office Make excel workbook readonly

bullpit

Thanks for that clarification. I really appreciate all your help. Is there a way to strongly protect the sheets so no one can modify them instead of having sheets password protected Password protection is very basic (I know its not meant to make sheets unmodifiable) and is easy to break.

Any pointers on this

Thanks again,

bullpit





Re: Visual Studio Tools for Office Make excel workbook readonly

Ji Zhou – MSFT

I know that, but as mentioned in the FAQ, if you use Excel, protecting is the best way and also the most appropriate way for your scenario.

Thanks

Ji