Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks,

currently I stuck in the following problem and I need your help here please.

But first let me explain it:

I have an Excel Workbook where most Cells are read-only and locked with a password. You can open the workbook and edit some cells, exept from the read-only cells.

My application now should go through this excel file and search a specific string and replace it with another string.
The string, what I'm searching for is always in the writable cells. Never in the read-only cells. So opening the workbook, search for those strings and replace them, should easily happen without the need of the password.

But this did not work. So I thought, ok, just let me enter the password and add it in the code:

C#
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
xlWorkBook = xlApp.Workbooks.Open(filePath.ToString(), 0, false, 5, "myPassword", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);



But this also doesn't work. I'm getting something like:
"The command cannot be applied to a protected sheet."

So, how could I achieve this; to edit a protected workbook and save the changes. If possible I would appreciate if the saved workbook will also got some read-only (protected) cells?


Thanks in advance
Max
Posted
Comments
me.ajaykumar 10-Sep-13 3:07am    
try this
workbook.Unprotect(pwd);
Lauryx 10-Sep-13 3:18am    
Unfortunately this doesn't work for me. I'm using Unprotect(pwd) right after opening the Workbook. But there is still the same Exception..
me.ajaykumar 10-Sep-13 3:21am    
http://msdn.microsoft.com/en-us/library/vstudio/7czw9wzs.aspx
see if you find any help on this link

1 solution

You Need to unprotect Sheet which you want to edit
like
sheet1.unprotect(Password);
 
Share this answer
 
Comments
Maciej Los 24-Oct-14 6:52am    
Who down-vote this answer?
This is very good answer. Please see: http://msdn.microsoft.com/en-us/library/office/ff841143%28v=office.15%29.aspx
+5!
Lauryx 24-Oct-14 14:15pm    
Although I don't work on this project anymore, I appreciate your answer and will try it soon. I have only a workaround yet.
So obviously it works better with "sheet.unprotect(pw)" than "workbook.unprotect(pw)"?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900