簡體   English   中英

Excel VBA-從Excel而不是從VBA傳遞字符串參數

[英]Excel VBA - string param pass from excel but not from VBA

我有DLL函數,它具有兩個字符串參數和兩個int參數。 該函數返回一個字符串。 當我直接從excel調用該函數時,可以,但是當我從VBA調用該函數時,給DLL函數的是傳遞字符串參數 ,該參數與原始字符(廢話字符) 不對應 並且函數返回字符串每隔一秒鍾就變成字符“”

我的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調用函數中這樣:(並且它僅返回字符串的第一個char)

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM