Thursday 9 July 2015

COM error: invalid number of parameters

Well, I've met last days a strange error message when I wanted to import data from Excel.
I'm not sure (haven't tested that) but the same problem may occur with other applications connected by COM (Outlook for example).

Error executing code: The method has been called with an invalid number of parameters

The problem was, that there was good number of parameters.

Method looked like that


    SysExcelApplication excelApp;
    SysExcelWorkbooks   excelWorkbooks;
    SysExcelWorksheets  excelWorksheets;
    SysExcelCells       excelCells;
    SysExcelWorksheet   excelWorksheet;
    ComVariant          cellValue;

....

    //line calling error
    cellValue   = excelCells.item(row, col).value();


....


I found workaround of that problem, which is just a....workaround, and nothing more. The problem is still there, and probably has to be fixed by Microsoft.


The main issue is, that this problem is not possible to replicate every time. Sometimes it is, and sometimes not.

I've added a line just before this line which is responsible for that error

     infolog.yield();

Yes, that's it!

There is another workaround for that which works, but I think that it does not give a guarantee that it will work - how can you know that 50 (or another number) will be enough?
There might a coincidence that it will work after 51st iteration.

Just add an error counter with maximum value, let's say 50.
After that, run this problematic line in try catch statement, iterating as many times as code will reach a limit gave as maximum (hardcoded)

Something like that

  Counter limitCounter;

  try
  {
    cellValue   = excelCells.item(row, col).value();
  }
  catch(Exception::Error)
  { 
     limitCounter++;

     if(limitCounter <= 50) //hardcoded!!
        retry;
     else
        throw(Exception::Error);
  } 


No comments:

Post a Comment