Search code examples
c#.netexceloffice-interop

Microsoft excel Evaluate function returns a code error (-2146826273)


I'm using microsoft.office.interop.excel dll in my application . when I execute the evaluation function with the below formula : =IF(100000>15000,20%,IF(100000>12500,17.5%,IF(100000>10000,15%,IF(100000>7500,12.5%,IF(100000>5000,10%,0)))))*IF((AND(40>=20,23>=30)),15,2000)*LOG(IF(AND(3<>0,(1/3)>0.5),1000,2000))/IF(90<>0, IF((1/90)>0.5, 123456, 987654), 987654)-550-IF((OR(25>=20, 1000>=30)), 5000, 900) . I get a code error -2146826273. I test the formula using excel sheet and it worked perfectly. Please find below the executed code.

 public dynamic EvaluteFormula(string expressionFormula)
     {
         Application excelApp = new Application();
         return excelApp.Evaluate(expressionFormula);
     }

When I minimize the formula length by removing operators, it works perfectly. Has anyone ideas how to solve this problem ?

Thanks!


Solution

  • The string parameter length must less or equal to 255 characters. Here is what MSDN states for the Application.Evaluate function which converts a Microsoft Excel name to an object or a value:

    A formula or the name of the object, using the naming convention of Microsoft Excel. The length of the name must be less than or equal to 255 characters.