Scriptorium

us fr nl




VBA : CommandBar: Creating a CommandBar with VBA   us


Options: Save as PDF | Save attached file | Toggle line numbers

Details:

Type: sub
Added By: Rembo
Short Description:
This sub procedure creates a CommandBar and adds a few buttons and popups. It is only to demonstrate how to make it work. Both buttons with a caption, an icon and both are displayed.
Notes:
Created for the JMT forum.
Added: Sep 8 2005 at 1:53 PM
Modified: Jun 13 2013 at 3:48 PM
Related URLs


Usage:

Run the procedure CreateCommandBar.


Code:

Formatted | Unformatted
  1. Sub CreateCommandBar()
  2. Dim myCB As CommandBar
  3. Dim myCBtn1 As CommandBarButton
  4. Dim myCBtn2 As CommandBarButton
  5. Dim myCPup1 As CommandBarPopup
  6. Dim myCPup2 As CommandBarPopup
  7. Dim myCP1Btn1 As CommandBarButton
  8. Dim myCP1Btn2 As CommandBarButton
  9. ' Delete the commandbar if it exists already
  10. On Error Resume Next
  11. Application.CommandBars("example").Delete
  12. ' Create a new Command Bar
  13. Set myCB = CommandBars.Add(Name:="example", Position:=msoBarFloating)
  14. ' Add button 1 to this bar
  15. Set myCBtn1 = myCB.Controls.Add(Type:=msoControlButton)
  16. With myCBtn1
  17. .Caption = "1st level Cap."
  18. .Style = msoButtonCaption '<- will force the caption text to show on your button
  19. End With
  20. ' Add popup menu 1 to this bar
  21. Set myCPup1 = myCB.Controls.Add(Type:=msoControlPopup)
  22. myCPup1.Caption = "Statistic"
  23. ' Add button 1 to popup menu 1
  24. Set myCP1Btn1 = myCPup1.Controls.Add(Type:=msoControlButton)
  25. With myCP1Btn1
  26. .Style = msoButtonAutomatic
  27. .FaceId = 487
  28. End With
  29. ' Add button 2 to popup menu 1
  30. Set myCP1Btn1 = myCPup1.Controls.Add(Type:=msoControlButton)
  31. With myCP1Btn1
  32. .Caption = "Click me!"
  33. .Style = msoButtonIconAndCaption
  34. .FaceId = 59
  35. .OnAction = "SubItworks"
  36. End With
  37. ' Add a second button to this bar
  38. Set myCBtn2 = myCB.Controls.Add(Type:=msoControlButton)
  39. With myCBtn2
  40. .FaceId = 17 ' <- Face Id 17 is a barchart icon
  41. .Caption = "Descriptive stat"
  42. End With
  43. ' Show the command bar
  44. myCB.Visible = True
  45. End Sub
  46.  
  47. Sub SubItworks()
  48. MsgBox ("Eureka, it works!")
  49. End Sub



User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website