If have an old Access DB front-end with hundreds of forms. I'm trying to update them en masse using VBA. I can do most things in VBA including open the form in design view, change the fonts and colors, but I can't figure out how to create or delete a control like a command button. Any help would be appreciated.
Public Sub ChgFormProps(Optional FormName As String, Optional ControlName As String)
Dim db As Database, Con As Container, Doc As Document
Dim frm As Form, ctl As Control, FormChgd As Boolean
Dim proNum As String, EM As errInfo: proNum = "001"
On Error GoTo ErrorHandler:
FormChgd = False
Debug.Print "Changed Forms:"
Set db = CurrentDb
For Each Con In db.Containers
For Each Doc In Con.Documents
If Con.Name = "Forms" Then
If FormName = Doc.Name Or FormName = "" Then
DoCmd.OpenForm Doc.Name, acDesign
Set frm = Screen.ActiveForm
For Each ctl In frm.Controls
If ControlName = ctl.Name Or ControlName = "" Then
Select Case ctl.ControlType
Case acComboBox
ctl.FontName = "Segoe UI"
FormChgd = True
Case acImage
Case acTabCtl
ctl.FontName = "Segoe UI"
FormChgd = True
Case acListBox
ctl.FontName = "Segoe UI"
FormChgd = True
Case acTextBox
ctl.FontName = "Segoe UI"
FormChgd = True
If ctl.Locked = True Then
ctl.BackColor = -2147483626
FormChgd = True
Else
ctl.BackColor = WHITE
FormChgd = True
End If
Case acCommandButton, acToggleButton
ctl.FontName = "Segoe UI"
If ctl.Name = "HelpCmd" Or ctl.Name = "CloseCmd" Then
'do nothing
Else
ctl.FontName = "Segoe UI"
ctl.ForeColor = BLACK
ctl.BackColor = WHITE
ctl.HoverColor = BLUE
ctl.HoverForeColor = WHITE
End If
FormChgd = True
Case acCheckBox
Case acLabel
ctl.FontName = "Segoe UI"
ctl.ForeColor = 0
FormChgd = True
If ctl.Caption = "DELETE" Then
Stop
End If
Case acLine
End Select
End If
Next
If FormChgd Then
DoCmd.Close acForm, Doc.Name, acSaveYes
Debug.Print Doc.Name
FormChgd = False
Else
DoCmd.Close acForm, Doc.Name, acSaveNo
End If
End If
End If
Next
Next
ExitHere:
Exit Sub
ErrorHandler:
Select Case Err
Case 2462
Resume Next
Case Else
EM = ErrMsg(Err, objNum, proNum)
fMsgBox EM.Msg, vbCritical, EM.Title
Resume ExitHere
End Select
End Sub
That worked. Use Application.DeleteControl and Application.CreateControl as suggested by HansUp. Thanks!
Related
Scenario
I have a word document where I have a table as shown in Image 1. The checkboxes are used to show the next contents. For example, I have in first step yes and no, when yes is checked the next content is shown. And in next step, I have thre Checkboxes with case 1,2 and 3 respectively.
When the case 1 is checked I have next a text that is filled via vba as F1Feld1...till F4Feld1.
Problem
First problem is, I am unable to create a function where only yes and no can be checked as well as either of the case can be checked. Second, problem is that the vba for case checkboxes run perfectly when I have them created separate but when combined together only case 1 vba runs.
Following is my code:
Option Explicit
Dim tabelle As Table, zelle As Cell
Private Sub Document_ContentControlOnEnter(ByVal CC As ContentControl)
Dim r As Range
Set tabelle = ActiveDocument.Bookmarks("local").Range.Tables(1)
If ActiveDocument.SelectContentControlsByTag("Yes").Item(1).Checked = True Then
ActiveDocument.SelectContentControlsByTag("No").Item(1).Checked = False
Call local_blockiert
Else: Call local_offen
End If
If ActiveDocument.SelectContentControlsByTag("Case1").Item(1).Checked = True Then
On Error Resume Next
With ActiveDocument
.Bookmarks("TB1").Range.Text = "F1Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB2").Range.Text = "F1Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB3").Range.Text = "F1Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB4").Range.Text = "F1Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
End With
ElseIf ActiveDocument.SelectContentControlsByTag("Case1").Item(1).Checked = False Then
With ActiveDocument
.Bookmarks("TB1").Range.Text = ""
.Bookmarks("TB2").Range.Text = ""
.Bookmarks("TB3").Range.Text = ""
.Bookmarks("TB4").Range.Text = ""
End With
ElseIf ActiveDocument.SelectContentControlsByTag("Case2").Item(1).Checked = True Then
With ActiveDocument
.Bookmarks("TB1").Range.Text = "F2Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB2").Range.Text = "F2Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB3").Range.Text = "F2Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB4").Range.Text = "F2Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
End With
ElseIf ActiveDocument.SelectContentControlsByTag("Case2").Item(1).Checked = False Then
With ActiveDocument
.Bookmarks("TB1").Range.Text = ""
.Bookmarks("TB2").Range.Text = ""
.Bookmarks("TB3").Range.Text = ""
.Bookmarks("TB4").Range.Text = ""
End With
ElseIf ActiveDocument.SelectContentControlsByTag("Case3").Item(1).Checked = True Then
With ActiveDocument
.Bookmarks("TB1").Range.Text = "F3Feld1": .Bookmarks("TB1").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB2").Range.Text = "F3Fed2": .Bookmarks("TB2").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB3").Range.Text = "F3Feld3": .Bookmarks("TB3").Range.Font.ColorIndex = wdBlack
.Bookmarks("TB4").Range.Text = "F3Feld4": .Bookmarks("TB4").Range.Font.ColorIndex = wdBlack
End With
ElseIf ActiveDocument.SelectContentControlsByTag("Case3").Item(1).Checked = False Then
With ActiveDocument
.Bookmarks("TB1").Range.Text = ""
.Bookmarks("TB2").Range.Text = ""
.Bookmarks("TB3").Range.Text = ""
.Bookmarks("TB4").Range.Text = ""
End With
End If
End Sub
Private Sub local_blockiert()
Dim i As Long, j As Long
On Error GoTo fehler
With ActiveDocument.Bookmarks("local").Range
.Font.ColorIndex = wdWhite
End With
fehler:
Call AllesAuf
End Sub
Private Sub local_offen()
Dim i As Long, j As Long
On Error GoTo fehler
With ActiveDocument.Bookmarks("YesorNo").Range
.Font.ColorIndex = wdBlack
End With
fehler:
Call AllesAuf
End Sub
Private Sub yes_blockiert()
Dim j As Long
On Error GoTo fehler
With tabelle.Cell(2, 2)
.Shading.ForegroundPatternColorIndex = wdGray25
.Range.Font.ColorIndex = wdGray25
For j = 1 To .Range.ContentControls.Count
.Range.ContentControls(j).LockContents = True
Next j
End With
Exit Sub
fehler:
Call AllesAuf
End Sub
Private Sub yes_offen()
Dim j As Long
On Error GoTo fehler
With tabelle.Cell(2, 2)
For j = 1 To .Range.ContentControls.Count
.Range.ContentControls(j).LockContents = False
Next j
.Shading.ForegroundPatternColor = RGB(255, 242, 204)
.Range.Font.ColorIndex = wdAuto
End With
Exit Sub
fehler:
Call AllesAuf
End Sub
Private Sub AllesAuf()
Dim i As Long
With ActiveDocument
For i = 1 To .ContentControls.Count
.ContentControls(i).LockContents = False
Next i
End With
End Sub
I'm pretty new at VBA. I have a form with a text box and a check box. If I open the form and click the check box without populating the text box, I get a message box to enter comments.
However, in the same session, if I unchecked the box and recheck it, I do not get the message box to enter comments even tho the text box is blank.
How can I get the code to "refire" in the same session to give me the message box to enter comments?
Below is my code:
Private Sub Check29_Click()
If IsNull(Me.Text16) Then
MsgBox "Comments are Required.", vbCritical
Me.Check29 = Null
Exit Sub
Else
If Me.Check29 = -1 Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16 = Null
Exit Sub
End If
End If
End Sub
I can see a logic problem in the code. Please be aware that this line:
Me.Check29 = Null
will re-trigger the _click event and produce unexpected results. And it is also incorrect (should be Me.Check29.Value=False). Please try the revised version below:
Declare a module level variable
Option Explicit
Private bCancel as Boolean
The event code (I also made more corrections):
Private Sub Check29_Click()
if bCancel Then Exit Sub
bCancel = False
If Trim(Me.Text16.Text) = vbnullstring Then
MsgBox "Comments are Required.", vbCritical
bCancel = True
Me.Check29.Value = False
bCancel = False
Exit Sub
Else
If Me.Check29.Value = True Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16.Text = vbnullstring
Exit Sub
End If
End If
End Sub
Private Sub Check29_Click()
If bCancel Then Exit Sub
bCancel = False
Me.Text16.SetFocus
If Trim(Me.Text16.Text) = vbNullString And Me.Check29.Value = True Then
MsgBox "Comments are Required.", vbCritical
bCancel = True
Me.Check29.Value = False
bCancel = False
Exit Sub
Else
If Me.Check29.Value = True Then
Dim RS As DAO.Recordset
Set RS = CurrentDb.OpenRecordset("Exclusions", dbOpenDynaset)
RS.AddNew
RS("HW535ID") = Me![HWID]
RS("Excluded") = "Yes"
RS("BOA Assignee") = Me![AssignedBA]
RS("Comments") = Me![Text16]
RS("CheckBox") = Me![Check29]
RS("Date of Exclusion") = Me![Text115]
RS("ReviewID") = Me![Text33]
RS.Update
RS.Close
Set RS = Nothing
Exit Sub
Else
If Me.Check29.Value = False Then
DoCmd.SetWarnings False
DoCmd.OpenQuery ("RemoveExclusion")
Me.Text16 = ""
Exit Sub
End If
End If
End If
End Sub
I want to determine if something has changed in a catpart that would drive a change in a body, but the change hasnt been done because update is set to manual.
I have tried:
if part.product.update = true then
'do something
else
'do something else
endif
However this just forces an update and doesnt tell me if one was required.
I worked out a solution (see below)
'#####################
Function func_CheckNoModsSinceLastSaved() As Boolean
func_CheckNoModsSinceLastSaved = False
Dim flg_NoModsSinceLastSaved As Boolean
flg_NoModsSinceLastSaved = False
If Right(CATIA.ActiveDocument.FullName, 11) = ".CATProduct" Then
Dim productDocument1 As ProductDocument
Set productDocument1 = CATIA.ActiveDocument
Dim product1 As Product
Set product1 = productDocument1.Product
flg_NoModsSinceLastSaved = productDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
product1.Update
flg_NoModsSinceLastSaved = productDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
Set productDocument1 = Nothing
ElseIf Right(CATIA.ActiveDocument.FullName, 8) = ".CATPart" Then
Dim partDocument1 As PartDocument
Set partDocument1 = CATIA.ActiveDocument
flg_NoModsSinceLastSaved = partDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
partDocument1.Part.Update
flg_NoModsSinceLastSaved = partDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
Set partDocument1 = Nothing
ElseIf Right(CATIA.ActiveDocument.FullName, 11) = ".CATDrawing" Then
Dim drawingDocument1 As DrawingDocument
Set drawingDocument1 = CATIA.ActiveDocument
flg_NoModsSinceLastSaved = drawingDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
drawingDocument1.Update
flg_NoModsSinceLastSaved = drawingDocument1.Saved
If flg_NoModsSinceLastSaved Then
func_CheckNoModsSinceLastSaved = True
Else
func_CheckNoModsSinceLastSaved = False
MsgBox "Modified Since Last Saved"
End If
Set drawingDocument1 = Nothing
Else
MsgBox "ERROR: Unidentified File Type!", vbCritical + vbOKOnly, ""
End If
End Function
'#####################
Check CATIA Settings
Set mySettControlers = CATIA.SettingControllers
Set myPartInfraSetting = mySettControlers.Item("CATMmuPartInfrastructureSettingCtrl")
Check how is set myPartInfraSetting.UpdateMode and see if is catManualUpdate or catAutomaticUpdate
I am facing issue with my macro, its running smoothly from desktop however when i put same file in network drive, other users facing Runtime error 91 Object variable or With block variable not set.
Also let me know how to remove protected view using macro.
Need your help for below code:
In Module
Sub UnhideAllSheets()
'Unhide all sheets in workbook.
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.Visible = xlSheetVisible
Next ws
End Sub
Sub HideAllSheets()
'Unhide all sheets in workbook.
Call UnhideAllSheets
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If InStr(1, ws.Name, "START", vbTextCompare) = 0 _
And InStr(1, ws.Name, "Data", vbTextCompare) = 0 Then
ws.Visible = xlVeryHidden
End If
Next ws
End Sub
Sub DRD()
Dim ws As Worksheet
Call HideAllSheets
'If VBA.Environ("Username") = "140736TJ" Then
'Sheets("8.Generator").Visible = xlSheetVisible
'Sheets("8.Generator").Select
With ActiveWorkbook.Worksheets
Select Case VBA.Environ("Username")
Case "130349TJ", "130355TJ"
Sheets("1.Large MCH").Visible = xlSheetVisible
Sheets("1.Large MCH").Select
Case "140646TJ"
Sheets("2.Large FAB").Visible = xlSheetVisible
Sheets("2.Large FAB").Select
Case "130361TJ"
Sheets("3.Blade").Visible = xlSheetVisible
Sheets("3.Blade").Select
Case "120243TJ"
Sheets("4.Nozzle").Visible = xlSheetVisible
Sheets("4.Nozzle").Select
Case "130360TJ", "146061tc"
Sheets("5.T.Assy").Visible = xlSheetVisible
Sheets("5.T.Assy").Select
Case "110206TJ"
Sheets("6.Rotor").Visible = xlSheetVisible
Sheets("6.Rotor").Select
Case "120237TJ"
Sheets("7. Control Valve").Visible = xlSheetVisible
Sheets("7. Control Valve").Select
Case "140736TJ", "110088TJ", "130344TJ"
Call UnhideAllSheets
Sheets("DRD Index Consolidation").Select
Case "120234TJ"
Call UnhideAllSheets
Sheets("DRD Index Consolidation").Select
Call StopDeleteRowCols
Case Else
MsgBox "ACCESS DENIED"
ActiveWorkbook.Close
Call ResetDeleteRowCols
End Select
End With
End Sub
Sub StopDeleteRowCols()
Dim ctl As CommandBarControl
For Each ctl In Application.CommandBars.FindControls(ID:=293)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=294)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=296)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=3181)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=292)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=3125)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=21)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=945)
ctl.Enabled = False
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=4)
ctl.Enabled = False
Next ctl
End Sub
Sub ResetDeleteRowCols()
Dim ctl As CommandBarControl
For Each ctl In Application.CommandBars.FindControls(ID:=293)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=294)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=296)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=3181)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=292)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=3125)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=21)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=945)
ctl.Enabled = True
Next ctl
For Each ctl In Application.CommandBars.FindControls(ID:=4)
ctl.Enabled = True
Next ctl
End Sub
Private Sub Workbook_Open()
DRD
End Sub
You should say what line you are getting the error. I'm sure the error comes from Application.CommandBars.FindControlsID:=?) not finding any controls. To trap the error I create a method Sub BatchEnableControls(Enable As Boolean) to find and Enable/Disable the controls. I also made a few other changes that will make the code easier to follow and expand.
Sub DRD()
Dim ws As Worksheet
Call HideAllSheets
'If VBA.Environ("Username") = "140736TJ" Then
'Sheets("8.Generator").Visible = xlSheetVisible
'Sheets("8.Generator").Select
Select Case VBA.Environ("Username")
Case "130349TJ"
Sheets("1.Large MCH").Visible = xlSheetVisible
Sheets("1.Large MCH").Select
Case "140646TJ"
Sheets("2.Large FAB").Visible = xlSheetVisible
Sheets("2.Large FAB").Select
Case "130361TJ"
Sheets("3.Blade").Visible = xlSheetVisible
Sheets("3.Blade").Select
Case "120243TJ"
Sheets("4.Nozzle").Visible = xlSheetVisible
Sheets("4.Nozzle").Select
Case "130360TJ"
Sheets("5.T.Assy").Visible = xlSheetVisible
Sheets("5.T.Assy").Select
Case "110206TJ"
Sheets("6.Rotor").Visible = xlSheetVisible
Sheets("6.Rotor").Select
Case "120237TJ"
Sheets("7. Control Valve").Visible = xlSheetVisible
Sheets("7. Control Valve").Select
Case "130355TJ"
Sheets("1.Large MCH").Visible = xlSheetVisible
Sheets("1.Large MCH").Select
Case "146061tc"
Sheets("5.T.Assy").Visible = xlSheetVisible
Sheets("5.T.Assy").Select
Case "140736TJ", "110088TJ", "130344TJ", "120234TJ"
Call UnhideAllSheets
Sheets("DRD Index Consolidation").Select
Case "120234TJ"
Call UnhideAllSheets
Sheets("DRD Index Consolidation").Select
BatchEnableControls False
Case Else
MsgBox "ACCESS DENIED"
BatchEnableControls True
ActiveWorkbook.Close
End Select
End Sub
Sub BatchEnableControls(Enable As Boolean)
EnableControls Enable, 293
EnableControls Enable, 294
EnableControls Enable, 296
EnableControls Enable, 3181
EnableControls Enable, 292
EnableControls Enable, 3125
EnableControls Enable, 21
EnableControls Enable, 945
EnableControls Enable, 4
End Sub
Sub EnableControls(Enable As Boolean, ControlID As Long)
Dim ctl As CommandBarControl
On Error Resume Next
For Each ctl In Application.CommandBars.FindControls(ID:=ControlID)
ctl.Enabled = Enable
Next ctl
If Err.Number <> 0 Then
Debug.Print "Error: " & Err.Description & " Could not find Control ID:=& ControlID "
End If
On Error GoTo zero
End Sub
i have 2 menu buttons that i want added in outlook menu after help menu. i made the code to add the buttons but it just adds 2 more buttons every time i reopen outlook even if the 2 menu buttons are there already . Any help is welcomed.
Function ToolBarExists(strName As String) As Boolean
Dim tlbar As commandBar
For Each tlbar In ActiveExplorer.CommandBars
If tlbar.Name = strName Then
ToolBarExists = True
Exit For
End If
Next tlbar
End Function
Sub TBarExistsbutton1()
If ToolBarExists("button1") Then
If ActiveExplorer.CommandBars("button1").Visible = True Then
ActiveExplorer.CommandBars("button1").Visible = False
Else
ActiveExplorer.CommandBars("button1").Visible = True
End If
Else
Call a123
End If
End Sub
Sub TBarExistsbutton2()
If ToolBarExists("button2") Then
If ActiveExplorer.CommandBars("button2").Visible = True Then
ActiveExplorer.CommandBars("button2").Visible = False
Else
ActiveExplorer.CommandBars("button2").Visible = True
End If
Else
Call a1234
End If
End Sub
Sub a123()
Dim outl As Object
Dim msg As Object
Set outl = CreateObject("Outlook.Application")
Dim objBar As Office.commandBar
Dim objButton As Office.commandBarButton
Set objBar = Application.ActiveWindow.CommandBars("Menu Bar")
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.caption = "button1"
.onAction = "macro1"
.faceId = 487
.Style = msoButtonIconAndCaption
End With
End Sub
Sub a1234()
Dim outl As Object
Dim msg As Object
Set outl = CreateObject("Outlook.Application")
Dim objBar As Office.commandBar
Dim objButton As Office.commandBarButton
Set objBar = Application.ActiveWindow.CommandBars("Menu Bar")
Set objButton = objBar.Controls.Add(msoControlButton)
With objButton
.caption = "button2"
.onAction = "macro2"
.faceId = 487
.Style = msoButtonIconAndCaption
End With
End Sub
In Outlook 2010. If Visible works for you incorporate it in a similar manner.
Option Explicit
Sub TBarExistsbutton1()
Dim cbControlCount As Long
Dim button1Found As Boolean
Dim j As Long
If ToolBarExists("Menu Bar") Then
cbControlCount = ActiveWindow.CommandBars("Menu Bar").Controls.count
Debug.Print " There are " & cbControlCount & " controls in " & "Menu Bar"
For j = 1 To cbControlCount
Debug.Print ActiveWindow.CommandBars("Menu Bar").Controls(j).Caption
If ActiveWindow.CommandBars("Menu Bar").Controls(j).Caption = "button1" Then
button1Found = True
Exit For
End If
Next j
If button1Found = False Then a123
Else
Debug.Print "Menu Bar does not exist."
a123
End If
End Sub