|
-
Mar 8th, 2006, 04:17 PM
#1
Thread Starter
Fanatic Member
[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:
Sub CmdBarTest()
Dim cbar As CommandBar, cbctl As CommandBarButton, i As Integer
Set cbar = Application.CommandBars.Add(Name:="Custom Toolbar6", _
Position:=msoBarFloating, MenuBar:=False, Temporary:=True)
cbar.Visible = True
For i = 1 To 6
With cbar.Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.Caption = "test" & i
.OnAction = "Action1(3)" 'Action1 fires twice when button clicked
' .OnAction = "Action2" 'Action2 fires once when button clicked
End With
Next i
End Sub
Sub Action1(i%)
Debug.Print "Action1 " & i
End Sub
Sub Action2()
Debug.Print "Action2"
End Sub
Last edited by VBAhack; Mar 8th, 2006 at 07:59 PM.
-
Mar 8th, 2006, 04:23 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 8th, 2006, 04:27 PM
#3
Thread Starter
Fanatic Member
Re: CommandBar button OnAction - pass parameter
Hi RobDog,
Same result.
VB Code:
Sub Action1(ByVal i%)
Debug.Print "Action1 " & i 'still fires twice
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.
-
Mar 8th, 2006, 04:35 PM
#4
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:
Sub CmdBarTest()
Dim cbar As CommandBar, cbctl As CommandBarButton, i As Integer
CommandBars("Custom Toolbar6").Delete
Set cbar = Application.CommandBars.Add(Name:="Custom Toolbar6", _
Position:=msoBarFloating, MenuBar:=False, Temporary:=True)
cbar.Visible = True
For i = 1 To 6
With cbar.Controls.Add(Type:=msoControlButton)
.Style = msoButtonCaption
.Caption = "test" & i
.Parameter = i
.OnAction = "Action1"
End With
Next i
End Sub
Sub Action1()
Dim ctlCBarControl As CommandBarControl
Dim i As Integer
Set ctlCBarControl = CommandBars.ActionControl
If ctlCBarControl Is Nothing Then Exit Sub
'Examine the Parameter property of the ActionControl to determine
'which control has been clicked
i = CInt(ctlCBarControl.Parameter)
Debug.Print "Action1 " & i
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
-
Mar 8th, 2006, 05:21 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 8th, 2006, 07:58 PM
#6
Thread Starter
Fanatic Member
Re: CommandBar button OnAction - pass parameter
 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.
-
Mar 9th, 2006, 01:38 AM
#7
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|