title: Set the Formula of an Excel Cell in a C# function
body: <p>I have created an Automation Add-in for Excel,implemented as a C# class library, which contains a UDF wrapper.(I do not use VSTO)</p>
  
  <p>What I would like to do in my UDF, is to:</p>
  
  <p>1>get the cell address where the formula was entered;</p>
  
  <p>2>set the formula of that cell to one of my own;</p>
  
  <p>I have already done point 1> as follows:</p>
  
  <pre><code>  Microsoft.Office.Interop.Excel.Application excelApp = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
  
    Microsoft.Office.Interop.Excel.Range target = (Microsoft.Office.Interop.Excel.Range)excelApp.get_Caller(System.Type.Missing);
  
    string cellAddress = target.get_Address(Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
  
    //Perform some operations based on address
  
    //Assign a formula to my cell (This does not work !)
    target.Formula ="=SUM(2,2)"
  </code></pre>
  
  <p>This should work according to the MSDN documentation from <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.formula%28v=office.14%29.aspx" rel="nofollow">here</a>.</p>
  
  <p>What am I doing wrong ? Is there any other way I can set the formula of the cell?</p>
  
  <p>Thanks,</p>