2

Basically trying to replicate the following VBA code in C++ I don't want my Excel Add-In to return an array (instead I need a UDF to change other cells) but the call to Excel4(xlcOnTime, &timer2, 2, &now, cmd) in the system timer function has a return code of 2, i.e., it failed to set the Excel's timer. Any idea? I am trying to limit my add-ins to 1 and in c++ code. I can solve the issue by having two add-ins, one in C++ and one in VBA but prefer not to.

namespace {
  UINT timer1 = 0;
  XLOPER timer2;
  XLOPER now;
};

//
//this function is xlfRegister as ApplicationOnTime
//
int __stdcall application_on_time()
{
  XLOPER ret;
  XLOPER ref;
  XLOPER value;
  //define the destination cells to be changed
  ref.xltype = xltypeSRef;
  ref.val.sref.count = 1;
  ref.val.sref.ref.rwFirst = 10;
  ref.val.sref.ref.rwLast = 12;
  ref.val.sref.ref.colFirst = 10;
  ref.val.sref.ref.colLast = 20;

  //the values
  value.xltype = xltypeMulti;
  value.val.array.columns = 2;
  value.val.array.rows = 3;
  value.val.array.lparray = new XLOPER[6];
  (value.val.array.lparray[0]).xltype = xltypeNum;
  (value.val.array.lparray[0]).val.num = 9991;
  (value.val.array.lparray[1]).xltype = xltypeNum;
  (value.val.array.lparray[1]).val.num = 9992;
  (value.val.array.lparray[2]).xltype = xltypeNum;
  (value.val.array.lparray[2]).val.num = 9993;
  (value.val.array.lparray[3]).xltype = xltypeNum;
  (value.val.array.lparray[3]).val.num = 9994;
  (value.val.array.lparray[4]).xltype = xltypeNum;
  (value.val.array.lparray[4]).val.num = 9995;
  (value.val.array.lparray[5]).xltype = xltypeNum;
  (value.val.array.lparray[5]).val.num = 9996;

  //set the value to the cell
  int xlret = Excel4(xlSet, &ret, 2, &ref, &value);

  printf("xlret is %d", xlret);

  return 0;
}

//xlfRegister as a command TestUserCommand, to be
//called from a button on Excel, with VBA code
//Application.run("TestUserCommand")
int __stdcall test_user_command()
{
  LPXLOPER now = new XLOPER;
  LPXLOPER cmd = new XLOPER;
  cmd->xltype = xltypeStr;
  cmd->val.str = XLUtil::MakeExcelString("ApplicationOnTime");

  Excel4(xlfNow, now, 0, NULL);

  timer2.xltype = xltypeInt;
  timer2.val.num = 0;
  int xlret = Excel4(xlcOnTime, &timer3, 2, now, cmd);
  //xlret is 0, the ApplicationOnTime command would be triggered
  //and the cells K11:U13 value get set as expected
  printf("xlret is %d", xlret);
  return 0;
}

//xlfRegister as a function TestUserFunction, to be
//called from a cell formula on Excel
//=TestUserFunction(1)
char * __stdcall test_user_function(xloper *pxl)
{
  static bool first = true;
  if (first) {
    timer2.xltype = xltypeInt;
    timer2.val.num = 0;
    first = false;
  }

  if (timer1 > 0)
  {
    UINT tmp = timer1;
    timer1 = 0;
    KillTimer(NULL, tmp);
  }

  Excel4(xlfNow, &now, 0, NULL);

  //set the system timer to triggers the MyTimerProc,
  //and MyTimerProc is triggered successfully but no luck
  //in setting the second timer that is excel
  //inside MyTimerProc
  timer1 = SetTimer(NULL, 0, 1, (TIMERPROC)MyTimerProc);

  return 0;
}

//this is the procedure referred to in the SetTimer call
void CALLBACK MyTimerProc(HWND hwnd, UINT message, UINT idTimer, DWORD dwTime)
{
  if (timer1 > 0)
  {
    UINT tmp = timer1;
    timer1 = 0;
    KillTimer(NULL, tmp);
  }
  LPXLOPER12 cmd = new XLOPER12;
  cmd->xltype = xltypeStr;
  cmd->val.str = (XCHAR *)XLUtil::MakeExcelString("ApplicationOnTime");
  now.val.num = now.val.num + 10.0 / (60.0 * 60.0 * 24.0);
  int xlret = Excel4(xlcOnTime, &timer2, 2, &now, cmd);
  //xlret is 2, indicates invalid function, no luck. the ApplicationOnTime
  //command won't be triggered

  //try to call the application_on_time function to set the values to cell
  //to no effect
  application_on_time();

}
Community
  • 1
  • 1
Mubai
  • 21
  • 3

2 Answers2

1

The problem is that your C++ code is not exact equivalent of the VBA code from the other question. In VBA to run a command by Excel timer it is

Application.OnTime

which uses Excel OnTime Automation call. In C++ you have

Excel4(xlcOnTime, &timer2, 2, &now, cmd);

which is XLL interface function xlcOnTime. Excel is very restrictive about XLL function calls; they may be called only in specific contexts. For instance xlcOnTime may be invoked from a user defined Excel command, but it cannot be called from Windows timer callback.

What you need to do is to use exact equivalent of VBA code in C++, which means using Automation in C++. There are several examples how to use Automation with Excel in C++ available in MSDN and other sources. They very often use AutoWrap function to call Automation, so you would need something like

AutoWrap(DISPATCH_METHOD, &result, pXlApp, L"OnTime", 1, COleVariant("ApplicationOnTime"));

Automation calls do not have the same restrictions as xlc functions, so such a call made from a Windows timer callback will be accepted by Excel and your ApplicationOnTime command will be run.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
-1

I'm pretty sure the reason is that you can't call any Excel4 functions from a thread other than the calling thread. Because you're calling back from inside a TIMERPROC, you are probably in the event thread or some other time thread. This is admittedly a massive pain, but that's the way it is.

Jim Moores
  • 308
  • 3
  • 5