Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2006

Tip: Looking for answers? Try searching our database.

Toolbar Combobox initialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dan Kelly - 26 Oct 2006 16:43 GMT
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?
Greg Maxey - 26 Oct 2006 16:49 GMT
Post the code that you are currently using.

> I have a template which contains a number of Macros
>
[quoted text clipped - 10 lines]
>
> Any pointers?
Dan Kelly - 26 Oct 2006 17:15 GMT
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
Greg Maxey - 26 Oct 2006 18:17 GMT
Seems to me that you would put the SetComboList()
code in an Auto_New and Auto_Open routine in the template.

> 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
[quoted text clipped - 29 lines]
>     ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
> End Sub
Dan Kelly - 27 Oct 2006 10:09 GMT
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...

> Seems to me that you would put the SetComboList()
> code in an Auto_New and Auto_Open routine in the template.
Greg Maxey - 27 Oct 2006 13:21 GMT
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

> Thanks for that - Adding the AutoNew and AutoOpen code does indeed add the
> combobox to the command bar.
[quoted text clipped - 8 lines]
> > Seems to me that you would put the SetComboList()
> > code in an Auto_New and Auto_Open routine in the template.
Dan Kelly - 27 Oct 2006 13:44 GMT
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)

> Dan,
>
[quoted text clipped - 35 lines]
> > > Seems to me that you would put the SetComboList()
> > > code in an Auto_New and Auto_Open routine in the template.
Greg Maxey - 27 Oct 2006 14:11 GMT
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

> Cheers Greg
>
[quoted text clipped - 44 lines]
> > > > Seems to me that you would put the SetComboList()
> > > > code in an Auto_New and Auto_Open routine in the template.

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.