【问题标题】:Excel VBA - string param pass from excel but not from VBAExcel VBA - 字符串参数来自 excel 但不是来自 VBA
【发布时间】:2015-04-17 12:18:50
【问题描述】:

我有 DLL 函数,它有两个字符串参数和两个 int 参数。该函数返回一个字符串。当我直接从excel调用函数时是可以的,但是当我从VBA调用函数时,DLL函数是传递string params,与原始不对应 (废话字符)。和函数返回字符串,女巫有每秒钟的字符“”

我的 dll 函数如下所示:

BSTR __stdcall getPattern(int sex, int pad, BSTR * a, BSTR * b){
    ...
}

在 VBA 中声明函数:

Declare Function GetPattern _
Lib "myPathToFunction" Alias "GetPattern" (ByVal poh As Integer, ByVal pad As Integer, ByRef a As String, ByRef b As String) As String

在excel中我这样调用函数:(没关系)

=GetPattern(I5;C1;A1;B1)

从 VBA 调用函数是这样的:(它只返回字符串的第一个字符)

result = GetPattern(Range("I5").Value, Range("C1").Value, Cells(i, 1).Value, Cells(i, 2).Value)

【问题讨论】:

    标签: c++ string excel vba


    【解决方案1】:

    这可能是 VB 在 C 调用中将字符串转换为 ANSI 的问题。您可能需要显式传递一个字符串指针:

    Dim param_a As String
    Dim param_b As String
    
    param_a = Cells(i, 1).Value
    param_b = Cells(i, 2).Value
    result = GetPattern(Range("I5").Value, Range("C1").Value, _
                        StrPtr(param_a), StrPtr(param_b))
    

    【讨论】:

    • 这得到相同的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2014-09-12
    相关资源
    最近更新 更多