Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing word bookmark list, with pages reference 2

Status
Not open for further replies.
Sep 29, 2003
29
0
0
GB
Hi,

I am trying to do the following

Print a list of bookmarks in a document, with a page reference for each bookmark.

I can get a list ok, by running the following macro;

Sub BkMarkList()
Dim J As Integer

Selection.TypeParagraph
Selection.InsertBreak Type:=wdColumnBreak
Selection.TypeText Text:="Bookmark list for "
Selection.TypeText Text:=ActiveDocument.Name
Selection.TypeParagraph
For J = 1 To ActiveDocument.Bookmarks.Count
Selection.TypeText Text:=Chr(9)
Selection.TypeText Text:=ActiveDocument.Bookmarks(J).Name
Selection.TypeParagraph
Next J
Selection.InsertBreak Type:=wdColumnBreak
End Sub

however I need to have a page reference for each bookmark, and I have not a clue how to get the page reference. Any help appreciated.

Thanks.
 

You would be better not using the Selection so much, but the page number you want can be found from:
Code:
ActiveDocument.Bookmarks(J).Range.Information(wdActiveEndPageNumber)

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
this works. I stuck it in as;

Sub BkMarkList()
Dim J As Integer

Selection.TypeParagraph
Selection.InsertBreak Type:=wdColumnBreak
Selection.TypeText Text:="Bookmark list for "
Selection.TypeText Text:=ActiveDocument.Name
Selection.TypeParagraph
For J = 1 To ActiveDocument.Bookmarks.Count
Selection.TypeText Text:=Chr(9)
Selection.TypeText Text:=ActiveDocument.Bookmarks(J).Name
Selection.TypeText Text:=" Page "
Selection.TypeText Text:=ActiveDocument.Bookmarks(J).Range.Information(wdActiveEndPageNumber)
Selection.TypeParagraph
Next J
Selection.InsertBreak Type:=wdColumnBreak
End Sub


ps I don't know vb, so the too many selection statements comment has whistled straight over my head. Feel free to elaborate.

ps I took the code straight off an example I saw, as I really don't understand the nuts and bolts of VB semantics.
 

A brief elaboration as this isn't really a VBA Forum:

The Selection represents what you (the user) are working with on the screen. When you are working in code it is more efficient to work with a Range.

I'm not quite sure why are using Column Breaks - if it is a real requirement you will have to change this code (which inserts a manual Page break) slightly.

Code:
[blue]With ActiveDocument.Content [green]' A (built in) Range[/green]
    .InsertAfter Chr(12) [green]' Page Break[/green]
    .InsertAfter "Bookmark list for " & ActiveDocument.Name
    .InsertParagraphAfter
        
    For Each bkmk In ActiveDocument.Bookmarks
        .InsertAfter vbTab & bkmk.Name & vbTab & "Page " & bkmk.Range.Information(wdActiveEndPageNumber)
        .InsertParagraphAfter
    Next
End With[/blue]

Note that I have also changed your loop to use a For Each construct. If you have many bookmarks each time you reference one of them by number Word has to start counting from the beginning and it can get quite slow; using For Each ... sets a reference to the bookmark once and then just uses it.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
As Tony mentions Selection is the object describing what is on-screen. It uses the resources of the graphical interface, it has to figure out pixel elements, and draw them.

Say you use Selection, by selecting the third paragraph of a document. All sorts of resources are required to not only identify the document part (the third paragraph), but also the resources to highlight (Select) changes to the screen.

Range is a Word object that only uses numbers, a Start and an End. These are location numbers, by characters, from the start of the document. They can be used to identify a document part - which is what you actually want to do. Because they are numbers, not pixel elements, they are much more efficient, as crunching numbers is what computers do well. Further, using a Range, you can do all sorts of things behind the scene that a user frankly does not care about.

And as Tony points out (and demonstrates in his code), it is even better yet to use objects. By using a bookmark object you can say:

For Each bookmark (it does not matter how many there are)...do this. No counting is required, or performed.

This is much better than saying:

Bookmark 1...do this
Bookmark 2...do this
Bookmark 3...do this
etc. etc.

Counting for the above IS required.

Gerry
My paintings and sculpture
 
thanks for the feedback on VBA. Just some quick feedback on
wdActiveEndPageNumber. This works most of the time. It gets confused if the bookmarks are in tables, as it provides the wrong page number. This is a known bug with microsoft. ie see

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top