title: What's wrong with locking cells in Excel c#
body: <p>I tried the following code below with Excel 365.
  Well, it does not work. I tried a couple of varations. The locking does not seem to work.
  Either the cells are not locked at all, or they cannot be visibly selected but can still be edited.
  Something seems broken here.
  Anyone got a solution?
  I just want to lock certain cells and others not, very very simple actually.</p>
  
  <p>In the code below the result for me is that I can still edit the cells. ALL OF THEM!</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml.Linq;
  using Excel = Microsoft.Office.Interop.Excel;
  using Office = Microsoft.Office.Core;
  using Microsoft.Office.Tools.Excel;     
  
  namespace SampleVSTOAddIn
  {
      public partial class ThisAddIn
      {
  
          private void WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook wb)
          {
              Excel.Worksheet activeWorksheet = ((Excel.Worksheet)Application.ActiveSheet);
  
              activeWorksheet.Protect(UserInterfaceOnly: true);
              activeWorksheet.EnableSelection = Excel.XlEnableSelection.xlUnlockedCells;
  
              activeWorksheet.get_Range("A1").Value2 = "Locked";
              activeWorksheet.get_Range("B1").Value2 = "Unlocked";
              activeWorksheet.get_Range("A1").Style.Locked = true;
              activeWorksheet.get_Range("B1").Style.Locked = false;
          }
  
          private void InternalStartup()
          {
  
              this.Application.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(WorkbookOpen);
  
          }
      }
  }
  </code></pre>