Re: Custom Commandbar - Buttons On Commandbar
Ok here is how Nory helped me - he made my original code into an array code. For the purposes of this forum, the File menu will be shown.
Norie's code:
Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cMenu2 As CommandBarComboBox
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCustomMenu As CommandBarControl
Dim arrCaptions
Dim arrFaceID
Dim arrOnAction
Dim arrBeginGroup
Dim I As Long
On Error Resume Next
Set cbMainMenuBar = Application.CommandBars.Add("CamCustom")
foundFlag = False
For Each cb In CommandBars
If cb.Name = "CamCustom" Then
cb.Visible = True
cb.Position = msoBarTop
cb.Protection = msoBarNoChangeDock
foundFlag = True
End If
Next cb
If Not foundFlag Then
MsgBox "There is an error with the command bar. Please restart the application."
End If
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ WELL FILE MENU
Set cMenu1 = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
cMenu1.Caption = "Well &File"
arrCaptions = Array("&New File Ctrl+N", "New Lost &Inventory Report", _
"Monday &Report", "&Save Ctrl+S", "Save &As... Shft+Ctrl+A", _
"&Print Main Report Ctrl+P", "Pre&view Main Report", _
"Print/ Export/ E&mail Main Report Ctrl+M", _
"&Export/ Email Main Report Ctrl+E", "Print &Info Collection Sheet", _
"E&xit")
arrFaceID = Array(32, 372, 7, 3, 1, 4, 1446, 1, 1, 4, 478)
arrOnAction = Array("Open_New_Book", "Open_New_LIM", "Go_To_Mon", "Save_Current", _
"Save_As", "Print_Main", "Prev_main", "Print_Export_Main", _
"Export_main", "Print_Info", "close_all")
arrBeginGroup = Array(False, False, False, True, False, True, False, False, False, True, True)
For I = LBound(arrCaptions) To UBound(arrCaptions)
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = arrCaptions(I)
.FaceId = arrFaceID(I)
.OnAction = arrOnAction(I)
.BeginGroup = arrBeginGroup(I)
End With
Next I
cbMainMenuBar.Visible = True
End Sub
Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("CamCustom").Delete
On Error GoTo 0
End Sub
Display More
My original code:
Sub AddMenus()
Dim cMenu1 As CommandBarControl
Dim cMenu2 As CommandBarComboBox
Dim cbMainMenuBar As CommandBar
Dim iHelpMenu As Integer
Dim cbcCustomMenu As CommandBarControl
On Error GoTo resumetask
Application.CommandBars("CamCustom").Delete
resumetask:
Set cbMainMenuBar = Application.CommandBars.Add("CamCustom")
foundFlag = False
For Each cb In CommandBars
If cb.Name = "CamCustom" Then
cb.Visible = True
cb.Position = msoBarTop
cb.Protection = msoBarNoChangeDock
foundFlag = True
End If
Next cb
If Not foundFlag Then
MsgBox "There is an error with the command bar. Please restart the application."
End If
On Error Resume Next
Application.CommandBars("CamCustom").Controls("&Well File").Delete
On Error GoTo 0
Set cbMainMenuBar = Application.CommandBars("CamCustom")
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ WELL FILE MENU
Set cMenu1 = cbMainMenuBar.Controls.Add(Type:=msoControlPopup)
cMenu1.Caption = "&File"
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "&New New File Ctrl+N"
.FaceId = 32
.OnAction = "Open_New_Book"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "New Lost &Inventory Report"
.FaceId = 372
.OnAction = "Open_New_LIM"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "Monday &Report"
.FaceId = 7
.OnAction = "Go_To_Mon"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "&Save Ctrl+S"
.FaceId = 3
.OnAction = "Save_Current"
.BeginGroup = True
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "Save &As... Shft+Ctrl+A"
.FaceId = 1
.OnAction = "Save_As"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "&Print Main Report Ctrl+P"
.FaceId = 4
.OnAction = "Print_Main"
.BeginGroup = True
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "Pre&view Main Report"
.FaceId = 1446
.OnAction = "Prev_main"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "Print/ Export/ E&mail Main Report Ctrl+M"
.FaceId = 1
.OnAction = "Print_Export_Main"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "&Export/ Email Main Report Ctrl+E"
.FaceId = 1
.OnAction = "Export_main"
.BeginGroup = False
End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "Print &Info Collection Sheet"
.FaceId = 4
.OnAction = "Print_Info_Collect"
.BeginGroup = True
End With
' Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
' With cbcCustomMenu
' .Caption = "&Close"
' .FaceId = 1
' .OnAction = "App_Close"
' .BeginGroup = True
' End With
Set cbcCustomMenu = cMenu1.Controls.Add(Type:=msoControlButton)
With cbcCustomMenu
.Caption = "E&xit"
.FaceId = 478
.OnAction = "Close_All"
.BeginGroup = True
End With
Sub DeleteMenu()
On Error Resume Next
Application.CommandBars("CamCustom").Controls("&Well File").Delete
On Error GoTo 0
End Sub
Display More
Now, back to something, anyone know how to begin group on a commandbar so it looks like:
*() simulates button
File Edit View Window Help |<begin group> (Bold)(Underline) |<begin group> (Align Left)