久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

C# - Excel Copy and Paste Formula in Protecte...

 sirianyan 2010-09-22

Hi,

I am working on a windows service, which will open a Excel file in In-Visible mode. Then it will add some data to some sheets and perform calculations using formulas in excel and get the result. This all working fine, but i am facing a problem regarding Copy & Paste formula in Hidden and Protected sheet and protected work book.

Here is my code so far:

//Copy the first cell which has formula

Excel.

 

Range copyRange = sheet.get_Range(FIRST_CELL, FIRST_CELL);

sheet.Unprotect(

 

 

"dsfdfdsfds");

copyRange.Select();

copyRange.Copy(

 

 

Type.Missing); \\EXCEPTION HERE

 

 

 

//Find last cell in this sheet

 

 

int lastRow = ((Excel.Range)sheet.Cells[7, 3]).get_End(Excel.XlDirection.xlDown).Row;

 

 

 

//Paste formula into all the "A" column cells

Excel.

 

Range pasteRange = sheet.get_Range(START_AT_CELL, "A" + lastRow.ToString());

pasteRange.PasteSpecial(Excel.

 

 

XlPasteType.xlPasteFormulas, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);

In the above code, i getting exception at copyRange.Copy(Type.Missing);

Exception: Select Method of range class failed.

Please help.

 

 

 

Answers

  • Thursday, September 16, 2010 2:27 AMAmy LiMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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./b/vsod/archive/2009/09/11/issue-change-in-behavior-from-office-2007-rtm-to-sp2-when-sheet-unprotect-is-called.aspx
    Hope 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.

All Replies

  • Thursday, September 16, 2010 2:27 AMAmy LiMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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./b/vsod/archive/2009/09/11/issue-change-in-behavior-from-office-2007-rtm-to-sp2-when-sheet-unprotect-is-called.aspx
    Hope it will be helpful. Thank you!

    Best Regards,
    Amy Li

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多