Search code examples
excelqtactivexvba

Call user defined VBA functions in Excel from Qt


As known, in Qt (or other C++ environment) we can use QActiveX to manipulate Excel. But if there are some user defined VBA functions in a workbook, any way to pass arguments to and call them?


Solution

  • You can call QAxObject::dynamicCall with the method name and arguments.

    Worksheet.Evaluate and Application.Evaluate are easy to use. They take one string argument, so you need to compose a script in the form of UDFName(parameter1,parameter2, ...). And because the string to UDF parameter type conversion, they are slower than Application.Run.

    Application.Run can also be used to call UDF. The first argument is the UDF name, and the rest of parameters are passed to the UDF by position.