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:
cb

nAction = "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