none
Excel Cell - readonly RRS feed

Answers

  • Hi saurabh,

    You need to mark the cell as locked and then protect the whole worksheet.

          try
          {
            Excel.Worksheet firstWks;
            firstWks = (Excel.Worksheet)this.Worksheets[1];
    
            /// Unlock all cells.
            firstWks.Cells.Locked = false;
    
            /// Lock cell A1.
            Excel.Range cellAOne;
            cellAOne = firstWks.get_Range("A1",missing);
            cellAOne.Locked = true;
    
            /// Protect the worksheet. Choose optional arguments according 
            /// to your needs.
            cellAOne.Value2 = "This cell is protected!";
            firstWks.Protect("secret",
                     true,
                     true,
                     true,
                     missing,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false);
          }
          catch (Exception ex)
          {
            Debug.Print(ex.Message);
          }
    
    

    Kind regards,

    Carlos Mallen

    Thursday, April 7, 2011 1:13 AM

All replies

  • Hi saurabh,

    You need to mark the cell as locked and then protect the whole worksheet.

          try
          {
            Excel.Worksheet firstWks;
            firstWks = (Excel.Worksheet)this.Worksheets[1];
    
            /// Unlock all cells.
            firstWks.Cells.Locked = false;
    
            /// Lock cell A1.
            Excel.Range cellAOne;
            cellAOne = firstWks.get_Range("A1",missing);
            cellAOne.Locked = true;
    
            /// Protect the worksheet. Choose optional arguments according 
            /// to your needs.
            cellAOne.Value2 = "This cell is protected!";
            firstWks.Protect("secret",
                     true,
                     true,
                     true,
                     missing,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false,
                     false);
          }
          catch (Exception ex)
          {
            Debug.Print(ex.Message);
          }
    
    

    Kind regards,

    Carlos Mallen

    Thursday, April 7, 2011 1:13 AM
  • whether it has any alternative other than password?
    Thursday, April 7, 2011 2:44 PM
  • Hi saurabh,

    I think there's no alternative. You can see if a cell is protected by using:

    1. The Locked property of the range.

    2. The ProtectContents property of the worksheet.

    3. The Protection property of the worksheet.

    Kind regards,

    Carlos Mallen

    Thursday, April 7, 2011 4:28 PM