如何在vba中传递超过255个字符的参数?

时间:2016-06-22 12:17:46

标签: vba

我有一个按钮,我将String作为参数传递。如果字符串参数增加超过255个字符,则它不会获得任何值。如果string参数小于255个字符,则表示正常。

这是我的代码:

Dim parameterText As String
parameterText = "First Parameter Value | Third Parameter Value | Third Parameter Value"
Dim AdviceItem As CommandBarButton
Set AdviceItem = CategoryItem.Controls.Add(msoControlButton, , , , True)               
With AdviceItem
    .Caption = adviceText
    .Visible = True
    .Parameter = strParameter
    .OnAction = "myFunction"
End With

Sub myFunction()
    Dim parameters() As String
    ReDim parameters(3)
    Dim parameterText As String
    parameterText = Application.CommandBars.ActionControl.Parameter
    'parameterText is blank if there are more than 255 characters passed from above function
    MsgBox ("parameterText" & parameterText)
    parameters() = Split(parameterText, "|")
End Sub

有人可以建议我如何实现它吗?

2 个答案:

答案 0 :(得分:1)

不,不是。 VB字符串类型可以保存超过255个字符的数据。

可变长度字符串最多可包含大约20亿(2 ^ 31)个字符

固定长度的字符串可以包含1到大约64K(2 ^ 16)个字符。

对于SPLIT,您可以尝试如下

Dim LString As String
Dim LArray() As String

LString = "foobar.com"
LArray = Split(LString, ".")

MsgBox LArray(0)
MsgBox LArray(1)

注意:https://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx

如果您有所了解,请尝试使用以下代码。

Function Over255()

    Dim myArray(3)  As String '<<<<< not variant

    myArray(0) = String(300, "a")
    myArray(1) = String(300, "b")
    myArray(2) = String(300, "c")
    myArray(3) = String(300, "d")

    'Over255 = Application.Transpose(myArray())
    Over255 = TR(myArray)

End Function

'like Application.Transpose...
Function TR(arrIn) As String()
    Dim arrOut() As String, r As Long, ln As Long, i As Long

    ln = (UBound(arrIn) - LBound(arrIn)) + 1
    ReDim arrOut(1 To ln, 1 To 1)
    i = 1
    For r = LBound(arrIn) To UBound(arrIn)
        arrOut(i, 1) = arrIn(r)
        i = i + 1
    Next r
    TR = arrOut

End Function

答案 1 :(得分:1)

假设.parameter存在限制,您可以通过使用数组存储要显示的字符串来绕过此限制。

 Option Explicit

    Public AllParameters(100) As String
    Public AllAdviceTexts(100) as String


    Sub defineParameters()

    AllParameters(0) = "First Parameter Value"
    AllParameters(1) = "Third Parameter Value "
    AllParameters(2) = "Third Parameter Value "
    'etc
'set advice texts here too    

    End Sub


    Private Sub Workbook_Open()

    Dim AdviceItem As CommandBarButton
    Dim i As Integer
    Call defineParameters
    For i = 0 To 100

    Set AdviceItem = CategoryItem.Controls.Add(msoControlButton, , , , True)
    With AdviceItem
        .Caption = AllAdviceTexts(i)
        .Visible = True
        .Parameter = i
        .OnAction = "myFunction"
    End With

    Next i

    End Sub


    Sub myFunction()
        Dim parameterText As String
        Dim index As Integer
        index = Application.CommandBars.ActionControl.Parameter
        parameterText = AllParameters(index)
        MsgBox ("parameterText" & parameterText)
    End Sub