我想从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;}这是一个来自字段的实函数的简化版本,它可以返回一个字符串,一个双数组,甚至一个数组。当然,这里我只返回一个字符串,但这限制了我的UDF返回到LPXLOPER12的类型。
我可以成功地用xlfRegister注册我的函数,指定一个pxTypeText of "U$"。然后,我可以从Excel调用我的UDF:
=HelloWorld()而且起作用了!
如果我按照建议的here从VBA调用我的函数
Sub macro_test()
Dim hw As Variant
hw = Application.Run("D:\Path\MyAddIn.xll!HelloWorld")
End Sub我从Application.run收到一条错误消息:
运行时错误“1004”:应用程序定义的或对象定义的错误
如果我试图按建议的here从VBA调用函数,则
Private Declare PtrSafe Function HelloWorld Lib "C:\Path\MyAddIn.xll" () As Variant
Sub macro_test()
Dim hw As Variant
hw = HelloWorld()
End Sub我得到一个空的结果而不是"Hello World"。
我做错什么了?
杂项资料:
Application.Run),VBA不调用我的函数(我不能使用调试器进入我的函数)。xlbitDLLFree添加到xltype中时,函数xlAutoFree12不会被调用,这使我认为返回值在某种程度上没有被VBA正确理解。发布于 2017-12-12 10:48:07
如果您的XLL被加载,并且它的UDF被注册,那么单元格中的that=HelloWord()就应该能够像这样从VBA调用它(除非没有参数的字符串函数有问题)
var=Application.run("HelloWorld")还可以使用“评估”。
var=Application.Evaluate("=HelloWorld()")我像这样测试了我的REVERSE.TEXT XLL函数,它正确地工作了。
Sub testing()
Dim var As Variant
var = Application.Run("REVERSE.TEXT", "Charles")
var = Application.Evaluate("=REVERSE.TEXT(""Charles"")")
End Sub使用UQQ$注册Reverse.Text (有两个参数,文本和字符数)
https://stackoverflow.com/questions/47729045
复制相似问题