PC Review


Reply
Thread Tools Rate Thread

Custom Toolbar Loading issue

 
 
Bishop
Guest
Posts: n/a
 
      2nd Jun 2009
I have the following code:

***On opening***

Private Sub Workbook_Open()

'The following code checks to see if the CDToolBar is present
'in Excel. If it isn't it puts it there. If it is it does nothing
Dim cbr As CommandBar

On Error Resume Next
Set cbr = Application.CommandBars("TSToolBar")
On Error GoTo 0

If cbr Is Nothing Then
Call TallySheetToolBar
Call AddCustomControl
End If

If ThisWorkbook.Name Like "Master*" Then NotSoFast.Show
End Sub

***Toolbar Code***

Sub TallySheetToolBar()

Dim TSToolBar As CommandBar

Set TSToolBar = CommandBars.Add(temporary:=True)
With TSToolBar
.Name = "TSToolBar"
.Position = msoBarTop
.Visible = True
End With
End Sub

***Add Button Code***

Sub AddCustomControl()

Dim CBar As CommandBar
Dim CTTally As CommandBarControl 'Catalyst To Tally
Dim PFNum As CommandBarControl 'PF Number
Dim CRData As CommandBarControl 'Clear Rep Data

Set CBar = CommandBars("TSToolBar")
Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
Set PFNum = CBar.Controls.Add(Type:=msoControlButton)
Set CRData = CBar.Controls.Add(Type:=msoControlButton)

With CTTally
.FaceId = 1763
.OnAction = "CatalystToTally"
End With

With PFNum
.FaceId = 643
.OnAction = "PFNumber"
End With

With CRData
.FaceId = 67
.OnAction = "ClearRepData"
End With

CBar.Visible = True

End Sub

Here is what's happening. Say I haven't loaded the toolbar yet. So I open
WB1 and the toolbar loads for the first time. I use the toolbar and it works
like it's supposed to. I close WB1 and open WB2. Once this toolbar loads it
stays in Excel. If I try to use the CTTally button it opens WB1 and executes
the code there. It never executes the CatalystToTally code for the current
WB (WB2) like it's supposed to. BUT, if I delete the custom toolbar, reload
it and reload the buttons it works fine. What would cause this behaviour?
If I change the WorkBook_Open procedure to delete the toolbar and reload it
everytime that (in theory) should do the trick but seems like a "hack" rather
than a fix. What causes the toolbar to "remember" the last WB?
 
Reply With Quote
 
 
 
 
jaf
Guest
Posts: n/a
 
      2nd Jun 2009
Hi,
What does .OnAction = "CatalystToTally" do?

Yes, it calls the sub CatalystToTally.
Where is that sub located? In wb1?
Does the sub use wb1 by name, Thisworkbook or activeworkbook?

John


"Bishop" <(E-Mail Removed)> wrote in message news:EBD6E61C-F9DA-4A2B-8718-(E-Mail Removed)...
>I have the following code:
>
> ***On opening***
>
> Private Sub Workbook_Open()
>
> 'The following code checks to see if the CDToolBar is present
> 'in Excel. If it isn't it puts it there. If it is it does nothing
> Dim cbr As CommandBar
>
> On Error Resume Next
> Set cbr = Application.CommandBars("TSToolBar")
> On Error GoTo 0
>
> If cbr Is Nothing Then
> Call TallySheetToolBar
> Call AddCustomControl
> End If
>
> If ThisWorkbook.Name Like "Master*" Then NotSoFast.Show
> End Sub
>
> ***Toolbar Code***
>
> Sub TallySheetToolBar()
>
> Dim TSToolBar As CommandBar
>
> Set TSToolBar = CommandBars.Add(temporary:=True)
> With TSToolBar
> .Name = "TSToolBar"
> .Position = msoBarTop
> .Visible = True
> End With
> End Sub
>
> ***Add Button Code***
>
> Sub AddCustomControl()
>
> Dim CBar As CommandBar
> Dim CTTally As CommandBarControl 'Catalyst To Tally
> Dim PFNum As CommandBarControl 'PF Number
> Dim CRData As CommandBarControl 'Clear Rep Data
>
> Set CBar = CommandBars("TSToolBar")
> Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
> Set PFNum = CBar.Controls.Add(Type:=msoControlButton)
> Set CRData = CBar.Controls.Add(Type:=msoControlButton)
>
> With CTTally
> .FaceId = 1763
> .OnAction = "CatalystToTally"
> End With
>
> With PFNum
> .FaceId = 643
> .OnAction = "PFNumber"
> End With
>
> With CRData
> .FaceId = 67
> .OnAction = "ClearRepData"
> End With
>
> CBar.Visible = True
>
> End Sub
>
> Here is what's happening. Say I haven't loaded the toolbar yet. So I open
> WB1 and the toolbar loads for the first time. I use the toolbar and it works
> like it's supposed to. I close WB1 and open WB2. Once this toolbar loads it
> stays in Excel. If I try to use the CTTally button it opens WB1 and executes
> the code there. It never executes the CatalystToTally code for the current
> WB (WB2) like it's supposed to. BUT, if I delete the custom toolbar, reload
> it and reload the buttons it works fine. What would cause this behaviour?
> If I change the WorkBook_Open procedure to delete the toolbar and reload it
> everytime that (in theory) should do the trick but seems like a "hack" rather
> than a fix. What causes the toolbar to "remember" the last WB?

 
Reply With Quote
 
Paul C
Guest
Posts: n/a
 
      2nd Jun 2009
I had a similar issue with a toolbar I used in several workbook. What I did
to fix any problems was delete the custom bar (if it exists, due to a crash
or something else) in my auto open and reload it for each workbook. On the
auto close I delete the tool bar when closing the workbook.

Sub Auto_Open()

' DELETE ANY OLD MENU THAT MAY HAVE BEEN LEFT FROM CRASH.
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Cost Rollup
Tools").Delete
On Error GoTo 0


' INSTALL CUSTOM MENU
Set newitem = Application.CommandBars("Worksheet Menu
Bar").Controls.Add(Type:= _
msoControlPopup, before:=11)
With newitem
.Caption = "&Cost Rollup Tools"
End With
'Any other commands

End Sub

Sub Auto_Close()

On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Cost Rollup
Tools").Delete
On Error GoTo 0
End Sub


As an alternative, you could create an .xla add on and have the tool bar
load when you open Excel. One down side to this method is that every user
who needs the tool bar would have to load the add in.
--
If this helps, please remember to click yes.


"Bishop" wrote:

> I have the following code:
>
> ***On opening***
>
> Private Sub Workbook_Open()
>
> 'The following code checks to see if the CDToolBar is present
> 'in Excel. If it isn't it puts it there. If it is it does nothing
> Dim cbr As CommandBar
>
> On Error Resume Next
> Set cbr = Application.CommandBars("TSToolBar")
> On Error GoTo 0
>
> If cbr Is Nothing Then
> Call TallySheetToolBar
> Call AddCustomControl
> End If
>
> If ThisWorkbook.Name Like "Master*" Then NotSoFast.Show
> End Sub
>
> ***Toolbar Code***
>
> Sub TallySheetToolBar()
>
> Dim TSToolBar As CommandBar
>
> Set TSToolBar = CommandBars.Add(temporary:=True)
> With TSToolBar
> .Name = "TSToolBar"
> .Position = msoBarTop
> .Visible = True
> End With
> End Sub
>
> ***Add Button Code***
>
> Sub AddCustomControl()
>
> Dim CBar As CommandBar
> Dim CTTally As CommandBarControl 'Catalyst To Tally
> Dim PFNum As CommandBarControl 'PF Number
> Dim CRData As CommandBarControl 'Clear Rep Data
>
> Set CBar = CommandBars("TSToolBar")
> Set CTTally = CBar.Controls.Add(Type:=msoControlButton)
> Set PFNum = CBar.Controls.Add(Type:=msoControlButton)
> Set CRData = CBar.Controls.Add(Type:=msoControlButton)
>
> With CTTally
> .FaceId = 1763
> .OnAction = "CatalystToTally"
> End With
>
> With PFNum
> .FaceId = 643
> .OnAction = "PFNumber"
> End With
>
> With CRData
> .FaceId = 67
> .OnAction = "ClearRepData"
> End With
>
> CBar.Visible = True
>
> End Sub
>
> Here is what's happening. Say I haven't loaded the toolbar yet. So I open
> WB1 and the toolbar loads for the first time. I use the toolbar and it works
> like it's supposed to. I close WB1 and open WB2. Once this toolbar loads it
> stays in Excel. If I try to use the CTTally button it opens WB1 and executes
> the code there. It never executes the CatalystToTally code for the current
> WB (WB2) like it's supposed to. BUT, if I delete the custom toolbar, reload
> it and reload the buttons it works fine. What would cause this behaviour?
> If I change the WorkBook_Open procedure to delete the toolbar and reload it
> everytime that (in theory) should do the trick but seems like a "hack" rather
> than a fix. What causes the toolbar to "remember" the last WB?

 
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
Adding custom buttons to the Standard toolbar versus a custom toolbar cainrandom@gmail.com Microsoft Outlook Program Addins 2 9th Oct 2008 05:13 PM
Saving custom toolbar or toolbar location to specific template Joseph N. Microsoft Word Document Management 0 16th Jan 2007 04:48 AM
Loading custom toolbar anonymous Microsoft Excel Misc 0 10th Mar 2005 07:07 PM
custom toolbar buttons are saved where? Excel loads twice bymistake and all my custom toolbar buttons get gone!!! Kevin Waite Microsoft Excel Programming 2 3rd Mar 2004 03:31 PM
custom toolbar buttons are saved where? Excel loads twice bymistake and all my custom toolbar buttons get gone!!! Kevin Waite Microsoft Excel Discussion 2 3rd Mar 2004 12:32 AM


Features
 

Advertising
 

Newsgroups
 


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