Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.OnAction won't call a function with a parameter 1

Status
Not open for further replies.

565u

Technical User
Apr 25, 2005
46
CZ
Hi and thanks for reading my post,
I've just spent half an afternoon looking thru advices on this and nothing works.
I have Word XP, I create a commandbar via VBA and then want to add a button that calls either a sub or a function with a parameter. That way I want to have a toolbar with several buttons that all call the same sub or function but each with a different parameter. Anything I try returns (when commandbar button clicked) "The macro cannot be found or has been disabled because of your Macro security settings."
Spomething is weird here, because calling a sub w/o a parameter works fine. Just for the record, my macro security is set to med, but i tested with low too with the same results.
I tested everything for example here: No results. Using the same code produces an error with me every time. I've read somewhere that this might be an MS error, that got later fixed, but I cannot be sure about it.

Many thanks advance for any kind help!!!
Best regards,
Pavel

My current code:
[tt]
Sub Test()
Dim xComBar As CommandBar
Set xComBar = Application.CommandBars.Add("testbar")
xComBar.Position = MsoBarPosition.msoBarTop
xComBar.Visible = True

Dim xComBarCntrl As CommandBarControl
Set xComBarCntrl = xComBar.Controls.Add(msoControlButton)
xComBarCntrl.Caption = "thebutton"
xComBarCntrl.Style = msoButtonCaption
xComBarCntrl.OnAction = "'Testit ""a"",""b""'"
End sub

Function testIt(x, y)
MsgBox x & ", " & y
End Function
[/tt]
 
Call subroutine without parameters. You can pick values from innside the sub in the same way you planned get parameters.

combo
 
Thank you for replying, but I dont understand: "You can pick values from innside the sub in the same way you planned get parameters."
my plan is to: "...add a button that calls either a sub or a function with a parameter. That way I want to have a toolbar with several buttons that all call the same sub or function but each with a different parameter. "

Best,
PH
 
Assuming your initial idea would be possible, you press button, the code starts and it needs arguments. How you planned to get parameters for your function/subroutine?

combo
 
Instead of trying to call with parameters, use the button's [blue][tt]Tag[/tt][/blue] or [blue][tt]Parameter[/tt][/blue] properties.

Something like this ..

[blue]
Code:
Sub Test()

    With Application.CommandBars.Add("testbar2")
    
        .Position = MsoBarPosition.msoBarTop
        .Visible = True
        
        With .Controls.Add(msoControlButton)
        
            .Caption = "thebutton"
            .Style = msoButtonCaption
            .OnAction = "Testit"
            .Tag = "ButtonTag"
            .Parameter = "Whatever"
        
        End With
        
    End With
    
End Sub

Function testIt() 

    With CommandBars.ActionControl
        MsgBox .Tag & vbTab & .Parameter
    End With

End Function
[/blue]


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
@combo
Thank you very much for your time, effort and good will. TonyJollans has just solved it.

@TonyJollans
A bow. A long bow.
Your solution is just beautiful and it works! I toyed around with .Parameter before but not in this way. The usage, the whole idea is just so nice!!! That's why YOU can call yourself the programmer! Wow, I am just blown away by this... it feels so good to experience a code like this. Thank you so much, Tony!! You've been just an incredible help!
Best regards!
Pavel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top