title: How do you set the value of a cell using Excel Dna?
body: <p>I've got the following code in my Excel DNA plugin</p>
  
  <p>In my AutoOpen method I put the following code:</p>
  
  <pre><code>ExcelIntegration.RegisterUnhandledExceptionHandler(ex =&gt; ex.ToString());
  </code></pre>
  
  <p>I've got the following function that gets called from my excel sheet.</p>
  
  <pre><code>[ExcelFunction(Category = "Foo", Description = "Sets value of cell")]
  public static Foo(String idx)
  {
          Excel.Application app = (Excel.Application)ExcelDnaUtil.Application;
          Excel.Workbook wb = app.Workbooks[1];
          Excel.Worksheet ws = GetSheet("Main");
  
          // This gives us the row
          Excel.Name idxRange = wb.Names.Item("COL_Main_Index");
          var row = (int)app.WorksheetFunction.Match(idx, idxRange.RefersToRange, 0);
  
          // Get the Column
          Excel.Name buyOrderRange = wb.Names.Item("COL_Main_BuyOrder");
          var col = (int)buyOrderRange.RefersToRange.Cells.Column;
  
          // create the range and update it
          Excel.Range r = (Excel.Range)ws.Cells[row, col];
          r.Value ="Foo"; 
  }
  </code></pre>
  
  <p>The issue is that I can't actually set any cell values.  When I call the method it causes an error on the last line.</p>
  
  <p>My error handler gives me the followign error:</p>
  
  <pre><code>{System.Runtime.InteropServices.COMException (0x800A03EC)
  </code></pre>
  
  <p>I've also tried to set the cell value like so:</p>
  
  <pre><code>        r = (Excel.Range)ws.Cells[12, 22];
          const int nCells = 1;
          Object[] args1 = new Object[1];
          args1[0] = nCells;
          r.GetType().InvokeMember("Value2", BindingFlags.SetProperty, null, r, args1);
  </code></pre>
  
  <p>With the same result.</p>
  
  <p>Can anyone point to what I might be doing wrong here?</p>