Try identifying the control with a Tag parameter:
With nCon
.BeginGroup = True
.Style = msoButtonCaption
.Caption = "Macro"
.OnAction = "RunMacro"
.Tag = "MyTag" '<<<< ADDED
End With
The text "MyTag" can be anything you want. Then, to delete the
controls, use
Dim C As Office.CommandBarControl
On Error Resume Next
Set C = Application.CommandBars.FindControl(Tag:="MyTag")
Do Until C Is Nothing
C.Delete
Set C = Application.CommandBars.FindControl(Tag:="MyTag")
Loop
This will delete all controls whose Tag property is "MyTag".
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
On Wed, 5 May 2010 11:57:01 -0700, Accesshelp
wrote:
Hello all,
I have a code that creates a command button when the Excel file opens. The
following is the code that I use:
Set nBar = CommandBars("Standard")
nBar.Visible = True
Set nCon = nBar.Controls.Add(Type:=msoControlButton, Temporary:=True)
With nCon
.BeginGroup = True
.Style = msoButtonCaption
.Caption = "Macro"
.OnAction = "RunMacro"
End With
What I would like to do is to remove the above command button "Macro" when
the Excel file closes. I have tried to use the following code, and it did
not work.
Application.CommandBars("Standard").Controls("Mac ro").Delete
Please help. Thanks.