PDA

View Full Version : How to sort list of bookmarks by location?



akokin
10-17-2008, 02:25 AM
Hello.
There is my own item menu with some commands (the code is below).
The first command "Sort by location" is intended for sorting. The second and subsequent commands represent the list of names of bookmarks (available in the document). These bookmarks are by default sorted alphabetically.

I need to get: on a clique on a command "Sort by location" the list of bookmarks in the menu will update their according to their placing in the document text. This command will has icon, showing to the user that this sorting is established. Repeated cliques on this command restores sorting alphabetically and remove the icon (FaceID).

To receive the list by position, it is necessary to replace the operator "For Each bm In ActiveDocument. Bookmarks" on "For Each bm In ActiveDocument. Range. Bookmarks". But here is how it to organise in a code by a call of command OnAction - I don't know.
Please help me. Thank you very much.

Sub MyMenu()
Dim cb As CommandBar
Dim cbP As CommandBarPopup
Dim cbbTop As CommandBarButton
Dim cbbBM As CommandBarButton
Dim bm As Bookmark

Set cb = CommandBars.ActiveMenuBar

Set cbP = CommandBars.FindControl(Tag:="Restore")
If Not cbP Is Nothing Then
cbP.Delete
End If

Set cbP = cb.Controls.Add(Type:=msoControlPopup, Before:=cb.Controls.Count + 1)
With cbP
.Caption = "My Menu"
.Tag = "Restore"
.BeginGroup = True
End With

Set cbbTop = cbP.Controls.Add(Type:=msoControlButton)
With cbbTop
.Caption = "Sort by location"
.OnAction = "bmSort"
End With

For Each bm In ActiveDocument.Bookmarks 'sort by alphabetically
Set cbbBM = cbP.Controls.Add(Type:=msoControlButton)
With cbbBM
.Caption = bm.Name
.Style = msoButtonCaption
End With
Next bm

Set cb = Nothing
Set cbbTop = Nothing
Set cbbBM = Nothing
End Sub

Sub bmSort()
'???
End Sub