从VBA调用xll UDF

Arn*_*aud 3 c++ excel vba excel-vba xll

我想从VBA调用我的一个用户定义函数.

我的用户定义函数在C++中声明:

XLOPER12*WINAPI HelloWorld()noexcept{
    THROWS();
    static XLOPER12 res=[](){
        static std::array<wchar_t,13>str={
            11,'H','e','l','l','o',' ','W','o','r','l','d','\0'};
        XLOPER12 tmp;
        tmp.xltype=xltypeStr;
        tmp.val.str=str.data();
        return tmp;}();
    return &res;}
Run Code Online (Sandbox Code Playgroud)

这是来自该字段的实际函数的简化版本,可以返回String或double或偶数数组.当然这里我只返回一个String,但是这限制了我的UDF的返回类型LPXLOPER12.

我可以成功注册我的函数xlfRegister指定pxTypeText"U$".然后我可以从Excel调用我的UDF:

=HelloWorld()
Run Code Online (Sandbox Code Playgroud)

它的工作原理!


如果我尝试按照此处的建议从VBA调用我的函数:

Sub macro_test()
    Dim hw As Variant
    hw = Application.Run("D:\Path\MyAddIn.xll!HelloWorld")
End Sub
Run Code Online (Sandbox Code Playgroud)

我从Application.run收到一条错误消息:

运行时错误'1004':应用程序定义的错误或对象定义的错误


如果我尝试按照此处的建议从VBA调用我的函数:

Private Declare PtrSafe Function HelloWorld Lib "C:\Path\MyAddIn.xll" () As Variant

Sub macro_test()
    Dim hw As Variant
    hw = HelloWorld()
End Sub
Run Code Online (Sandbox Code Playgroud)

我得到一个空结果而不是"Hello World".


我究竟做错了什么 ?

杂项信息:

  • 使用Excel 2013
  • 使用VS 2017 15.5
  • 使用第一个方法(Application.Run),VBA不会调用我的函数(我无法使用调试器进入我的函数).
  • 使用第二种方法,VBA调用我的函数(我可以使用调试器进入我的函数).
  • 使用第二种方法,当我添加xlbitDLLFree到my时xltype,函数xlAutoFree12不会被调用,这让我觉得VBA不能正确理解返回值.

Cha*_*ams 5

如果您的XLL已加载并且其UDF已注册,以便单元格中的= HelloWord()工作,那么您应该能够像这样从VBA调用它(除非无参数字符串函数存在问题)

var=Application.run("HelloWorld")
Run Code Online (Sandbox Code Playgroud)

您也可以使用Evaluate

var=Application.Evaluate("=HelloWorld()")
Run Code Online (Sandbox Code Playgroud)

我测试了我的REVERSE.TEXT XLL功能,它运行正常.

Sub testing()
Dim var As Variant
var = Application.Run("REVERSE.TEXT", "Charles")
var = Application.Evaluate("=REVERSE.TEXT(""Charles"")")
End Sub
Run Code Online (Sandbox Code Playgroud)

Reverse.Text使用UQQ $注册(有2个参数,Text和字符数)