【问题标题】:how to call xlcOnTime from xll如何从 xll 调用 xlcOnTime
【发布时间】:2016-03-30 19:19:24
【问题描述】:

基本上试图在 C++ 中复制以下 VBA 代码 I don't want my Excel Add-In to return an array (instead I need a UDF to change other cells) 但是在系统定时器函数中调用Excel4(xlcOnTime, &timer2, 2, &now, cmd)返回码为2,即设置Excel的定时器失败。 任何的想法?我试图将我的加载项限制为 1 和 c++ 代码。我可以通过两个加载项来解决这个问题,一个在 C++ 中,一个在 VBA 中,但我不想这样做。

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();

}

【问题讨论】:

    标签: excel xll


    【解决方案1】:

    问题在于您的 C++ 代码与其他问题中的 VBA 代码不完全相同。在 VBA 中通过 Excel 计时器运行命令是

    Application.OnTime
    

    使用 Excel OnTime 自动化调用。在 C++ 中你有

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

    这是XLL接口函数xlcOnTime。 Excel 对 XLL 函数调用非常严格;它们只能在特定的上下文中调用。例如,xlcOnTime 可以从用户定义的 Excel 命令中调用,但不能从 Windows 计时器回调中调用。

    您需要做的是在 C++ 中使用与 VBA 代码完全相同的代码,这意味着在 C++ 中使用自动化。在 MSDN 和其他来源中提供了几个如何在 C++ 中使用自动化与 Excel 的示例。他们经常使用AutoWrap 函数来调用自动化,所以你需要类似的东西

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

    自动化调用与 xlc 函数没有相同的限制,因此 Excel 将接受从 Windows 计时器回调发出的此类调用,并且将运行您的 ApplicationOnTime 命令。

    【讨论】:

      【解决方案2】:

      我很确定原因是您不能从调用线程以外的线程调用任何 Excel4 函数。因为您是从 TIMERPROC 内部回调,所以您可能在事件线程或其他时间线程中。诚然,这是一种巨大的痛苦,但事实就是如此。

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 2010-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-06
        • 1970-01-01
        • 2016-09-06
        相关资源
        最近更新 更多