question

YamenJeribi-2720 avatar image
0 Votes"
YamenJeribi-2720 asked Viorel-1 edited

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

Hello,

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!

dotnet-csharpoffice-excel-itpro
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
1 Vote"
Viorel-1 answered Viorel-1 edited

According to documentation, the length is limited to 255 characters. You can try using some worksheet and cell. For example:

 var excelApp = new Application( );
 var wb = excelApp.Workbooks.Add( Type.Missing );
 var ws = wb.Worksheets[1];
 var r = ws.Range["A1"];
 r.Formula = expressionFormula;
 var result = r.Value;

The result is -5549.9986630824169.



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.