Sub ListAllButtonsPopulateListBox()
'Code requires a userForm1 with ListBox1
Dim lX As Long
Dim lY As Long
Dim sPrintLine As String
Dim lNextWriteRow As Long
Dim aryButtonNames() As Variant
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Document.CmdBarAndBtns").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Worksheets.Add(before:=Sheets(1)).Name = "Document.CmdBarAndBtns"
lNextWriteRow = 1
For lX = 1 To CommandBars.Count
For lY = 1 To CommandBars(lX).Controls.Count
Cells(lNextWriteRow, 1) = CommandBars(lX).Controls(lY).Caption
'if you want to populate a list box then something line this:
ReDim Preserve aryButtonNames(1 To lNextWriteRow)
aryButtonNames(lNextWriteRow) = CommandBars(lX).Controls(lY).Caption
lNextWriteRow = lNextWriteRow + 1
Next
Next
'Next line will remove the & from each of the captions (used to mark shortcut keys)
Cells.Replace What:="&", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
UserForm1.ListBox1.List = aryButtonNames
UserForm1.Show
End Sub
bFound = False
For lX = CommandBars.Count To 1 Step -1
For lY = 1 To CommandBars(lX).Controls.Count
If CommandBars(lX).Controls(lY).Caption = "Access Scripts" Then
bFound = True
Exit For
End If
Next
If bFound Then
CommandBars(lX).Delete
bFound = False
End If
Next
Sub newTEST()
'Code requires a userForm1 with ListBox1
Dim lX As Long
Dim lY As Long
Dim sPrintLine As String
Dim lNextWriteRow As Long
Dim aryButtonNames() As Variant
Application.DisplayAlerts = False
On Error Resume Next
Worksheets("Document.CmdBarAndBtns").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Worksheets.Add(before:=Sheets(1)).Name = "Document.CmdBarAndBtns"
lNextWriteRow = 1
bFound = False
For lX = CommandBars.Count To 1 Step -1
For lY = 1 To CommandBars(lX).Controls.Count
If CommandBars(lX).Controls(lY).Caption = "Access Scripts" Then
bFound = True
Exit For
End If
Next
If bFound Then
[COLOR=black]CommandBars(lX).Delete ' FAILS HERE[/COLOR]
bFound = False
End If
Next
'Next line will remove the & from each of the captions (used to mark shortcut keys)
Cells.Replace What:="&", Replacement:="", LookAt:=xlPart, SearchOrder:= _
xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
UserForm1.ListBox1.List = aryButtonNames
UserForm1.Show
End Sub