combobox that can be seen every pages

A

albatros

Hi,
Lets say I have a word doc. which is 30 pages.
Every pages has its subject.

1-I would like to create a combobox, which includes the headings of these
pages.
So when I clicked any item in the combo, macro will automatically send me
the corresponding page (my intend is make a kind of table of content).

2-the important part is, like toolbars, this combo should stay at the same
position while scrolling, or changin the pages.
Have a good day.
 
H

Helmut Weber

Hi Albatros,

you need a modeless userform
with a combobox on it.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jean-Guy Marcil

albatros was telling us:
albatros nous racontait que :
Hi,
Lets say I have a word doc. which is 30 pages.
Every pages has its subject.

1-I would like to create a combobox, which includes the headings of
these pages.
So when I clicked any item in the combo, macro will automatically
send me the corresponding page (my intend is make a kind of table of
content).

2-the important part is, like toolbars, this combo should stay at the
same position while scrolling, or changin the pages.
Have a good day.

Here is some code that does what you want with a toolbar dropdown. It picks
up all the Heading 1 style paragraphs and places them in the dropdown, Then
you use the dropdown to navigate to any of them.

'_______________________________________
'Change the caption constant to suit your situation
Public Const ButtonCaption As String = "Heading 1 list"
Public Const ToolBarName As String = "Test"
'_______________________________________
Sub AddComboBox()
'With the template open, create a toolbar, call it "Test"
'(If you want a different name, call it something else
'and do not forget to change it as well in the constant
'declaration above so that all macros will work)
'Add the macro "FillHeadingList" below to the toolbar
'as the first button
'Tools > Customize, Commands tab, on the right select Macros
'On the left find the macro and drag it to the toolbar
'Right click on it and in the name area type: Update
'Then from VBA IDE, place the cursor in this text and press F5
'to execute this macro (AddComboBox)
'Finally, if you want, in the ThisDocument module,
'Create a Document_Open event
'Add to that event: DropList.FillHeadingList
'and a Document_New event if necessary as well
'So everytime the document will be opeend the list will
'update itself as well as when the user clicks on the
'Update button on the Test toolbar

Dim cbo As Office.CommandBarComboBox

Set cbo = CommandBars(ToolBarName). _
Controls.Add(Type:=msoControlComboBox)

cbo.Caption = ButtonCaption
'when user selects an item in the list,
'this macro will execute:
cbo_OnAction = "DropList.GoToHeading"
cbo.Width = 100

End Sub
'_______________________________________

'_______________________________________
Sub ResetHeadingList()

CommandBars(ToolBarName). _
Controls(ButtonCaption).Clear

End Sub
'_______________________________________

'_______________________________________
Sub FillHeadingList()

Dim cbo As Office.CommandBarComboBox
Dim bFound As Boolean
Dim CursorPos As Range

DropList.ResetHeadingList

Selection.Collapse wdCollapseStart

Set CursorPos = Selection.Range

Set cbo = CommandBars(ToolBarName). _
Controls(ButtonCaption)
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Do
With Selection.Find
.Format = True
.Style = wdStyleHeading1
.Forward = True
.Wrap = wdFindStop
.Text = ""
.Execute
bFound = .Found
If bFound Then cbo.AddItem Left(Selection.Text, _
Len(Selection.Text) - 1)
Selection.Collapse wdCollapseEnd
End With
'In case last para is Heading 1, exit loop
Loop While bFound And _
Selection.End + 1 < ActiveDocument.Range.End
'Instead of a Do...Loop, you could have a bunch of
' cbo.AddItem staement to fill your bar with static names
'In this case, you do not need the Update button or
'the Document_Open event
'Just run this macros from the VBA IDE once

CursorPos.Select

End Sub
'_______________________________________

'_______________________________________
Sub GoToHeading()

Dim cbo As Office.CommandBarComboBox
Dim rng As Word.Range
Set cbo = CommandBars(ToolBarName). _
Controls(ButtonCaption)
Set rng = ActiveDocument.Range

With rng.Find
.Style = wdStyleHeading1
.Text = cbo.Text
.Execute
End With
rng.Select
'or something like:
' Selection.TypeText cbo.Text
Selection.Collapse wdCollapseStart
End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

albatros

Thank you Helmut Weber,
I had not think userform. If I didn't cope with the following code I will
use userform. Thank you very much. good days.


"Helmut Weber":
 
A

albatros

Hi again, thank you for this quick answer.
I am trying to follow your code but because of I had not study with constant
before, and when I copy your;
Public Const ButtonCaption As String = "Heading 1 list"
Public Const ToolBarName As String = "Test"

both of them are red, there is an error: "Constantsi fÅŸxed-length strings,
arrays, user defined types and Declare statements not allowed as Public
numbers of object modules.


I put this codes into the general/declerations of the ThisDocument. Am I
wrong doing this ?

good days.
 
J

Jean-Guy Marcil

albatros was telling us:
albatros nous racontait que :
Hi again, thank you for this quick answer.
I am trying to follow your code but because of I had not study with
constant before, and when I copy your;
Public Const ButtonCaption As String = "Heading 1 list"
Public Const ToolBarName As String = "Test"

both of them are red, there is an error: "Constantsi fsxed-length
strings, arrays, user defined types and Declare statements not
allowed as Public numbers of object modules.


I put this codes into the general/declerations of the ThisDocument.
Am I wrong doing this ?

Yes, put the whole thing in a standard module.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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