Results 1 to 7 of 7

Thread: [RESOLVED] CommandBar button OnAction - pass parameter

  1. #1

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    [RESOLVED] CommandBar button OnAction - pass parameter

    Is it legal to pass parameter values to the sub triggered by OnAction? I tried it and for some reason the sub associated with OnAction fires twice when clicked whereas the parameterless version only fires once. Why twice?

    VB Code:
    1. Sub CmdBarTest()
    2.     Dim cbar As CommandBar, cbctl As CommandBarButton, i As Integer
    3.     Set cbar = Application.CommandBars.Add(Name:="Custom Toolbar6", _
    4.         Position:=msoBarFloating, MenuBar:=False, Temporary:=True)
    5.     cbar.Visible = True
    6.     For i = 1 To 6
    7.         With cbar.Controls.Add(Type:=msoControlButton)
    8.             .Style = msoButtonCaption
    9.             .Caption = "test" & i
    10.             .OnAction = "Action1(3)"        'Action1 fires twice when button clicked
    11. '            .OnAction = "Action2"          'Action2 fires once when button clicked
    12.         End With
    13.     Next i
    14. End Sub
    15.  
    16. Sub Action1(i%)
    17.     Debug.Print "Action1 " & i
    18. End Sub
    19.  
    20. Sub Action2()
    21.     Debug.Print "Action2"
    22. End Sub
    Last edited by VBAhack; Mar 8th, 2006 at 07:59 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CommandBar button OnAction - pass parameter

    What if you pass it ByVal instead of the default ByRef?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: CommandBar button OnAction - pass parameter

    Hi RobDog,

    Same result.

    VB Code:
    1. Sub Action1(ByVal i%)
    2.     Debug.Print "Action1 " & i       'still fires twice
    3. End Sub

    There'd be a straightforward workaround such as toggling a flag, but seems strange that it would do this. I'm using Excel 2002 by the way.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: CommandBar button OnAction - pass parameter

    VBAHAck
    You can use the Parameter property of the control to achieve this result without the dupes.


    VB Code:
    1. Sub CmdBarTest()
    2.     Dim cbar As CommandBar, cbctl As CommandBarButton, i As Integer
    3.    
    4.     CommandBars("Custom Toolbar6").Delete
    5.     Set cbar = Application.CommandBars.Add(Name:="Custom Toolbar6", _
    6.         Position:=msoBarFloating, MenuBar:=False, Temporary:=True)
    7.     cbar.Visible = True
    8.     For i = 1 To 6
    9.         With cbar.Controls.Add(Type:=msoControlButton)
    10.             .Style = msoButtonCaption
    11.             .Caption = "test" & i
    12.             .Parameter = i
    13.             .OnAction = "Action1"
    14.         End With
    15.     Next i
    16. End Sub
    17.  
    18. Sub Action1()
    19. Dim ctlCBarControl  As CommandBarControl
    20. Dim i As Integer
    21.    
    22.     Set ctlCBarControl = CommandBars.ActionControl
    23.     If ctlCBarControl Is Nothing Then Exit Sub
    24.     'Examine the Parameter property of the ActionControl to determine
    25.     'which control has been clicked
    26.     i = CInt(ctlCBarControl.Parameter)
    27.    
    28.     Debug.Print "Action1 " & i
    29. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: CommandBar button OnAction - pass parameter

    lol I forgot and didnt even double check the button props.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: CommandBar button OnAction - pass parameter

    Quote Originally Posted by DKenny
    Set ctlCBarControl = CommandBars.ActionControl
    Brilliant! Never heard of this before. Thanks!

    http://msdn.microsoft.com/library/de...HV05221249.asp
    Last edited by VBAhack; Mar 9th, 2006 at 12:09 AM.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [RESOLVED] CommandBar button OnAction - pass parameter

    But if you use WithEvents of the Office.CommandBarButton class you dont need to select caase as each procedure will tie to ech WithEvents used.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width