PC Review


Reply
Thread Tools Rate Thread

Adding and Deleting custom commandbars

 
 
=?Utf-8?B?d2h5bGl0ZQ==?=
Guest
Posts: n/a
 
      10th Sep 2007
The code I have currently in an addin is below. What I am finding is that
sometimes Excel doesn't close properly or for any other reason the adding of
these command bars becomes duplicated. I have had to remove up to twenty on
some of my staffs computers. How can I write this so that it checks to see
if the command bar exists and if it does to not add it again?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar"). _
Controls("Tools").Controls("Import DR Data File").Delete
Application.CommandBars("Worksheet Menu Bar"). _
Controls("Tools").Controls("Daily Revenue Reset").Delete
End Sub

Private Sub Workbook_Open()

Set newmenuitem = Application.CommandBars _
("Worksheet Menu Bar").Controls("Tools").Controls.Add
With newmenuitem
.Caption = "Import DR Data File"
.FaceId = 312
.BeginGroup = True
.OnAction = "MorningReport"
End With
Set newmenuitem = Application.CommandBars _
("Worksheet Menu Bar").Controls("Tools").Controls.Add

With newmenuitem
.Caption = "Daily Revenue Reset"
.FaceId = 1678
.BeginGroup = False
.OnAction = "reset_morning_reports"
End With

End Sub
--
Thanks!
Shane W
 
Reply With Quote
 
 
 
 
Robert Bruce
Guest
Posts: n/a
 
      10th Sep 2007

"whylite" <(E-Mail Removed)> wrote in message
news:C7531FF8-CFAF-4958-8B58-(E-Mail Removed)...
> The code I have currently in an addin is below. What I am finding is that
> sometimes Excel doesn't close properly or for any other reason the adding
> of
> these command bars becomes duplicated. I have had to remove up to twenty
> on
> some of my staffs computers. How can I write this so that it checks to
> see
> if the command bar exists and if it does to not add it again?
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> On Error Resume Next
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Import DR Data File").Delete
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Daily Revenue Reset").Delete
> End Sub
>
> Private Sub Workbook_Open()
>
> Set newmenuitem = Application.CommandBars _
> ("Worksheet Menu Bar").Controls("Tools").Controls.Add


Try calling Workbook_BeforeClose(False) as the first line of your
Workbook_Open.

Rob

 
Reply With Quote
 
=?Utf-8?B?TWlrZSBI?=
Guest
Posts: n/a
 
      10th Sep 2007
Why not delete it first just in case it exists?

On Error Resume Next
Application.CommandBars("mybar").Delete

Mike

"whylite" wrote:

> The code I have currently in an addin is below. What I am finding is that
> sometimes Excel doesn't close properly or for any other reason the adding of
> these command bars becomes duplicated. I have had to remove up to twenty on
> some of my staffs computers. How can I write this so that it checks to see
> if the command bar exists and if it does to not add it again?
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> On Error Resume Next
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Import DR Data File").Delete
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Daily Revenue Reset").Delete
> End Sub
>
> Private Sub Workbook_Open()
>
> Set newmenuitem = Application.CommandBars _
> ("Worksheet Menu Bar").Controls("Tools").Controls.Add
> With newmenuitem
> .Caption = "Import DR Data File"
> .FaceId = 312
> .BeginGroup = True
> .OnAction = "MorningReport"
> End With
> Set newmenuitem = Application.CommandBars _
> ("Worksheet Menu Bar").Controls("Tools").Controls.Add
>
> With newmenuitem
> .Caption = "Daily Revenue Reset"
> .FaceId = 1678
> .BeginGroup = False
> .OnAction = "reset_morning_reports"
> End With
>
> End Sub
> --
> Thanks!
> Shane W

 
Reply With Quote
 
JE McGimpsey
Guest
Posts: n/a
 
      10th Sep 2007
One way:

Note that you're adding custom Controls, not custom CommandBars...

First, make sure you set the Temporary argument to True in the
..Controls.Add method.


Second, you can delete leftover controls before adding their
replacements with something like:

With Application.CommandBars("Worksheet Menu Bar")
On Error Resume Next
.FindControl(Tag:="ImportDRControl").Delete
.FindControl(Tag:="DailyRevenueReset").Delete
On Error GoTo 0
With .Controls
With .Add(Type:=msoControlButton, Temporary:=True)
.Caption = "Import DR Data File"
.FaceId = 312
.BeginGroup = True
.OnAction = "MorningReport"
.Tag = "ImportDRControl"
End With
With .Add(Type:=msoControlButton, Temporary:=True)
.Caption = "Daily Revenue Reset"
.FaceId = 1678
.BeginGroup = False
.OnAction = "reset_morning_reports"
.Tag = "DailyRevenueReset"
End With
End With
End With

Third - if it's possible that the user might have a custom menu bar, you
might want to substitute

Application.CommandBars.ActiveMenuBar

for

Application.CommandBars("Worksheet Menu Bar")

In article <C7531FF8-CFAF-4958-8B58-(E-Mail Removed)>,
whylite <(E-Mail Removed)> wrote:

> The code I have currently in an addin is below. What I am finding is that
> sometimes Excel doesn't close properly or for any other reason the adding of
> these command bars becomes duplicated. I have had to remove up to twenty on
> some of my staffs computers. How can I write this so that it checks to see
> if the command bar exists and if it does to not add it again?
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> On Error Resume Next
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Import DR Data File").Delete
> Application.CommandBars("Worksheet Menu Bar"). _
> Controls("Tools").Controls("Daily Revenue Reset").Delete
> End Sub
>
> Private Sub Workbook_Open()
>
> Set newmenuitem = Application.CommandBars _
> ("Worksheet Menu Bar").Controls("Tools").Controls.Add
> With newmenuitem
> .Caption = "Import DR Data File"
> .FaceId = 312
> .BeginGroup = True
> .OnAction = "MorningReport"
> End With
> Set newmenuitem = Application.CommandBars _
> ("Worksheet Menu Bar").Controls("Tools").Controls.Add
>
> With newmenuitem
> .Caption = "Daily Revenue Reset"
> .FaceId = 1678
> .BeginGroup = False
> .OnAction = "reset_morning_reports"
> End With
>
> End Sub

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom toolbar adding and deleting Palpha32 Microsoft Excel Programming 4 8th Jul 2008 05:53 AM
CommandBars & Custom =?Utf-8?B?Sm9obiBKQk0=?= Microsoft Excel Programming 1 16th May 2007 05:44 PM
deleting CommandBars Mark J. McGinty Microsoft Outlook Program Addins 5 5th Jul 2006 03:07 PM
custom commandbars Wes Peters Microsoft Access Forms 4 26th Nov 2004 10:14 PM
Custom faces for custom menus/commandbars Stu Valentine Microsoft Excel Programming 1 17th Sep 2004 04:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:14 AM.