Toolbar Combobox initialization

D

Dan Kelly

I have a template which contains a number of Macros

Simply speaking I have one Macro that creates and populates a Combobox on a
CommandBar. Selecting an item from the Combobox in turn runs other Macros.

This all works apart from one thing.

At the moment I have to Manually run the initial "SetComboList" macro in
order to create the Combobox.

I cannot figure out how to get the combobox to generate when the template is
opened (I only want the drop list to appear for this one template)

Any pointers?
 
D

Dan Kelly

The following is all contained in its own Module ("Colour") within the "A3
Report.dot" template. For brevity I have only shown one of the subroutines
called as they all work fine.

Dim cb As CommandBarComboBox
' Set the combobox
Sub SetComboList()
Set cb = CommandBars("Colour").Controls.Add(msoControlComboBox)
With cb
.OnAction = "SetColour"
.Style = msoComboNormal
.Width = 150
.AddItem "Green"
.AddItem "Purple"
End With
End Sub
' Set Colours for the base line depending on selection
Private Sub SetColour()
Select Case cb.Text
Case "Green"
Green
Case "Purple"
Purple
End Select
End Sub
' Change Line colour to Green
Private Sub Green()
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.HeaderFooter.Shapes("Line 3").Select
Selection.ShapeRange.Line.ForeColor.RGB = RGB(176, 209, 55)
Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
 
G

Greg Maxey

Seems to me that you would put the SetComboList()
code in an Auto_New and Auto_Open routine in the template.
 
D

Dan Kelly

Thanks for that - Adding the AutoNew and AutoOpen code does indeed add the
combobox to the command bar.

However, I am now asked when I close the resulting document whether I want
to save changes to the template.

If I say yes to the template change then the combobox gets added to the
template - hence next time I open the document (or create a new one) there
are 2 comboboxes, and so on...
 
G

Greg Maxey

Dan,

I seems to me that after you have added the combo box to the template
then you would simply leave it there and remove the code. Perhaps I am
missing your point.

To prevent the autoopen and autonew macros from adding a control that
is already there your could give your control a caption and then if
that control is found when the the macro runs it would bypass creation
of a second duplicate control:

Sub Auto_Open()
Dim cbb As CommandBarControl
For Each cbb In CommandBars("Colour").Controls
If cbb.Caption = "Color Selector" Then Exit Sub
Next
Set cb = CommandBars("Colour").Controls.Add(msoControlComboBox)
With cb
.Caption = "Color Selector"
.OnAction = "SetColour"
.Style = msoComboNormal
.Width = 150
.AddItem "Green"
.AddItem "Purple"
End With
End Sub
 
D

Dan Kelly

Cheers Greg

If I remove the code then would the "OnAction" code persist?

At the moment I am fooling the Template into thinking that it has been saved
after I add the combobox, which seems to work OK and provides the option of
ammending the list at a later date (For instance the colour names at the
moment haven't been approved by PR and might change)
 
G

Greg Maxey

Dan,

No not as currently written because the variable cb is not set in the
SetColour procedures.

Consider this approach. Build your combobox using the BuildControl code
and save the template.

Dim cb As CommandBarComboBox
Sub BuildControl()
'Run once. Save the template and delete this routine
Set cb = CommandBars("Colour").Controls.Add(msoControlComboBox)
With cb
.Caption = "Color Selector"
.OnAction = "SetColour"
.Style = msoComboNormal
.Width = 150
.AddItem "Green"
.AddItem "Purple"
End With
End Sub

Now use this code to set the colours. Unstet the commented out code
and run it once to find the index number of the control you previously
built. I assumed it was "1", but if there are more than one control on
the command bar you will need to know which index number to use in the
..Contols(?) statement.


Private Sub SetColour()
'Dim cbc As CommandBarControl
'For Each cbc In CommandBars("Colour").Controls
' MsgBox cbc.Caption & " " & cbc.Index
'Next
Set cb = CommandBars("Colour").Controls(1)
Select Case cb.Text
Case "Green"
MsgBox "Green"
Case "Purple"
MsgBox "Purple"
End Select
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top