How to sort list of bookmarks by location?

A

avkokin

Hello.
There is list of bookmarks into menu made with the macro (see:
http://word.mvps.org/FAQs/Customization/BookmarksToMenu.htm).
By default the list is sort alphabetically. For sort by location I
need to change line "For Each oBookmark In ActiveDocument.Bookmarks"
on "For Each oBookmark In ActiveDocument.Range.Bookmarks". But how to
do it with help the additional item into this menu? For example, the
item menu "Bookmarks" has one additional item "Sort by location"
besides the list of bookmarks. By click on this item should sort of
list by location. By repeat click on this item - restore sort
alphabetically. How to do it?
Thank you very much.
 
J

Jonathan West

avkokin said:
Hello.
There is list of bookmarks into menu made with the macro (see:
http://word.mvps.org/FAQs/Customization/BookmarksToMenu.htm).
By default the list is sort alphabetically. For sort by location I
need to change line "For Each oBookmark In ActiveDocument.Bookmarks"
on "For Each oBookmark In ActiveDocument.Range.Bookmarks". But how to
do it with help the additional item into this menu? For example, the
item menu "Bookmarks" has one additional item "Sort by location"
besides the list of bookmarks. By click on this item should sort of
list by location. By repeat click on this item - restore sort
alphabetically. How to do it?
Thank you very much.

If I understand you, you want an additional item at the top of the menu
which offers you a Sort by Name/Sort by Location toggle for the order of the
menu items below. Is that correct?

If so, then you need to modify things a bit. First of all, your menu with
the first button will be there permanently, rather then being generated by
the macro itself.

Then, the macro (which will be triggered by clicking the Sort button) will
have to be modified so that it does the following

- It checks whether the current order is by name or location.
- It deletes all the existing items on the menu (except for the sort button
itself). Use the Delete method for this
- It recreates all the buttons according to the desired order

The code in the macro already in the article has about 70% of what you need.

See how you get on, and come back here if you get stuck.
 
A

avkokin

Hello Jonathan. Thank you for help. But I can't understand how to
organize such check-up. And I wrote this full macro-code which add
item menu an has my item "Sort by location" (below). Please explain me
for it. And how to add the separator below my item but above list of
bookmarks? It should be on the top.
Thank you.

Sub CreateBookMarkMenu()
Dim oBookmark As Bookmark
Dim oBar As CommandBar
Dim oPopup As CommandBarPopup
Dim oButton As CommandBarButton
Dim oButSort As CommandBarButton
Dim ShowHiddenStatus As Boolean

ShowHiddenStatus = ActiveDocument.Bookmarks.ShowHidden
ActiveDocument.Bookmarks.ShowHidden = False
CustomizationContext = ActiveDocument
Set oBar = CommandBars.ActiveMenuBar

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

If ActiveDocument.Bookmarks.Count > 0 Then
Set oPopup = oBar.Controls.Add(Type:=msoControlPopup, _
Before:=oBar.Controls.Count + 1)
With oPopup
.Caption = "Bookmarks"
.Tag = "Recreate"
End With

'Add a Sort button at the top
Set oButSort = oPopup.Controls.Add(Type:=msoControlButton)

With oButSort
.Caption = "Sort by location"
.FaceId = 29
.Style = msoButtonCaption
.OnAction = "bmSort"
End With

'Show all exist bookmark sort alphabetically
For Each oBookmark In ActiveDocument.Bookmarks
Set oButton = oPopup.Controls.Add(Type:=msoControlButton)

With oButton
' .BeginGroup = True
.Caption = oBookmark.Name
.Style = msoButtonCaption
.OnAction = "BookMarkSelect"
.Tag = oBookmark.Name
End With
Next oBookmark

'Add a Refresh button at the bottom
Set oButton = oPopup.Controls.Add(Type:=msoControlButton)

With oButton
.Caption = "Refresh list"
.Style = msoButtonCaption
.OnAction = "CreateBookMarkMenu"
.BeginGroup = True
End With
End If
ActiveDocument.Bookmarks.ShowHidden = ShowHiddenStatus
Set oButton = Nothing
Set oPopup = Nothing
Set oBar = Nothing
Set oBookmark = Nothing
End Sub

Sub BookMarkSelect()

ActiveDocument.Bookmarks(CommandBars.ActionControl.Tag).Range.Select
End Sub

Sub bmSort()
'????
End Sub
 
K

Klaus Linke

I think it's easier than that.

ActiveDocument.Bookmarks is sorted by name.
ActiveDocument.Content.Bookmarks is sorted by the order in which the
bookmarks appear (... which is what you want).

No sorting necessary... At least, it has always worked for me.

Regards,
Klaus
 
K

Klaus Linke

Oops, didn't read the original post, and that you know that already.
I guess Jonathan meant to create the list using either
For Each oBookmark In ActiveDocument.Content.Bookmarks

or
For Each oBookmark In ActiveDocument.Bookmarks
depending on the options setting (for which I'd use two -- mutually
exclusive -- radio buttons "Sort by location" and "Sort by name").

Regards,
Klaus
 

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