Hi RedRoseASP,
I've made the program in VS2010 and Excel2010. It's a Workbook Project. After running it, it won't throw
such an exception. So I'm not sure if I have missed something. Here is the code snippet. Please check to see if
it will solve the problem.
private void Sheet1_Startup(object sender, System.EventArgs e) { Excel.Application thisApp=this.Application; Excel.Workbook mybook = thisApp.Workbooks[1] as Excel.Workbook; mybook.Protect(); Excel.Worksheet sheet=thisApp.Worksheets[1] as Excel.Worksheet; sheet.Protect("aaa", this.ProtectDrawingObjects, true, this.ProtectScenarios, this.ProtectionMode, this.Protection.AllowFormattingCells, this.Protection.AllowFormattingColumns, this.Protection.AllowFormattingRows, this.Protection.AllowInsertingColumns, this.Protection.AllowInsertingRows, this.Protection.AllowInsertingHyperlinks, this.Protection.AllowDeletingColumns, this.Protection.AllowDeletingRows, this.Protection.AllowSorting, this.Protection.AllowFiltering, this.Protection.AllowUsingPivotTables); Excel.Range copyRange = sheet.get_Range("D1","D1"); sheet.Unprotect("aaa"); copyRange.Select(); copyRange.Copy(System.Type.Missing); int lastRow = ((Excel.Range)sheet.Cells[7, 3]).get_End(Excel.XlDirection.xlDown).Row; string START_AT_CELL = "A1"; Excel.Range pasteRange = sheet.get_Range(START_AT_CELL, "A" + lastRow.ToString()); ; pasteRange.PasteSpecial(Excel.XlPasteType.xlPasteFormulas, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false); }
In the Sheet1 of Workbook I've set the value of B1 to 1 and C1 to 4. Add the SUM formula to D1. Then copy the
formula in D1 to all the A columns of the Sheet1. If I misunderstood you or any further questions, please feel free
to contact. Also here is a link about the issue of "Change in behavior from Office2007 RTM to SP2 when Sheet.Unprotect is called":
http://blogs.msdn.com/b/vsod/archive/2009/09/11/issue-change-in-behavior-from-office-2007-rtm-to-sp2-when-sheet-unprotect-is-called.aspxHope it will be helpful. Thank you!
Best Regards,
Amy Li
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.