none
Runtime error 5 Invalid procedure call or argument, for a string pulled from an array in Word 2007 RRS feed

  • Question

  • Hello. I have been writing a little code for Word and I have reached the limits of my understanding. I had found code that would add macros to context menus in Word. I used the brute force method of writing the code below for each menu. I am now trying to make it a little more elegant with arrays and subs. When I try to pull the name from the array I created to the sub that generates the menu item I get the error. If I just set the string to "Text" it works

    Why does

    Sub Dupsy() 'Add paste unformatted to context menu ' On Error GoTo Bye Dim Bore, N Dim Bare As String Bore = Array("Text", "Table Text") For N = LBound(Bore) To UBound(Bore) ' Bare = Chr(34) & Bore(N) & Chr(34)

    Bare = "Text" Call Bars(Bare) Next N Bye: End Sub Sub Bars(Bare) Dim cb As CommandBar Dim ctl As CommandBarButton Dim Bara As String ' On Error GoTo Bye CustomizationContext = NormalTemplate” Set cb = CommandBars(Bare) Set ctl = cb.FindControl(Tag:="PstUnf") If ctl Is Nothing Then Set ctl = cb.Controls.Add(Type:=msoControlButton, Before:=4, Temporary:=True) With ctl .Caption = "Paste Unformatted" .Tag = "PstUnf" .OnAction = "PstUnf" End With End If End Sub

    Work when

    Sub Dupsy() 'Add paste unformatted to context menu ' On Error GoTo Bye Dim Bore, N Dim Bare As String Bore = Array("Text", "Table Text") For N = LBound(Bore) To UBound(Bore) Bare = Chr(34) & Bore(N) & Chr(34)

    ' Bare ="Text" Call Bars(Bare) Next N Bye: End Sub Sub Bars(Bare) Dim cb As CommandBar Dim ctl As CommandBarButton Dim Bara As String ' On Error GoTo Bye CustomizationContext = NormalTemplate Set cb = CommandBars(Bare) ‘Runtime Error 5 at this step MsgBox Bare gives “Text” Set ctl = cb.FindControl(Tag:="PstUnf") If ctl Is Nothing Then Set ctl = cb.Controls.Add(Type:=msoControlButton, Before:=4, Temporary:=True) With ctl .Caption = "Paste Unformatted" .Tag = "PstUnf" .OnAction = "PstUnf" End With End If End Sub


    Does not?

    In the case that fails I am pulling the string “Text” from the array Bore. It errors immediately when Bare = "Text" and never gets to the second array member.

    Am pulling what little hair I have left over this.

    Thanks in advance

    Blake

    Thursday, April 12, 2012 3:05 AM

Answers

  • The line

        Bare = Chr(34) & Bore(N) & Chr(34)

    causes the double quotes " = Chr(34) to be part of the string Bare. In the first example, the value of Bare is Text, in the second example it is "Text" including the quotes.

    Change the line to

        Bare = Bore(N)


    Regards, Hans Vogelaar

    • Marked as answer by Blakearino Thursday, April 12, 2012 3:16 PM
    Thursday, April 12, 2012 5:35 AM

All replies

  • The line

        Bare = Chr(34) & Bore(N) & Chr(34)

    causes the double quotes " = Chr(34) to be part of the string Bare. In the first example, the value of Bare is Text, in the second example it is "Text" including the quotes.

    Change the line to

        Bare = Bore(N)


    Regards, Hans Vogelaar

    • Marked as answer by Blakearino Thursday, April 12, 2012 3:16 PM
    Thursday, April 12, 2012 5:35 AM
  • Hans

    Thank you for the quick reply. You are correct it does work that way. I think I changed to the double quotes to see if that would fix another problem and forgot to change back. Too much error not enough trial.

    Thanks

    Blake

    Thursday, April 12, 2012 7:21 PM