Identify page on which a bookmark is located



I have code (obtained from this group in response to an earlier question) to
print only the part of a document between two bookmarks. The following
works inconsistently. Details are after the code. The code itself is in an
Add-In.

Dim x1 As Long ' start character of bookmark
Dim x2 As Long ' end character of bookmark
Dim p1 As Long ' start page
Dim p2 As Long ' end page

Dim rTmp As Range
Set rTmp = ActiveDocument.Range
' Allow for a bookmark at the beginning of document
If rTmp.Bookmarks("DocStart").Range.Start = 0 Then
x1 = 1
Else
x1 = rTmp.Bookmarks("DocStart").Range.Start
End If
x2 = rTmp.Bookmarks("DocEnd").Range.End
p1 = rTmp.Characters(x1).Information(wdActiveEndPageNumber)
p2 = rTmp.Characters(x2).Information(wdActiveEndPageNumber)

ActiveDocument.PrintOut _
Range:=wdPrintFromTo, From:=CStr(p1), To:=CStr(p2)

Set rTmp = Nothing

When the bookmarks DocStart and DocEnd are on the same page (so that only
one page is printed, which is the case most of the time) it seems to work as
intended. However, in other situations the results are mixed at best. For
instance, DocStart is on page 7, but p1 is 9 when I step through the code.
In the same document, if DocEnd is on page 8, it is identified as 11. That
is, the variables p1 and p2 are 9 and 11 rather than 7 and 8.
Another problem I have noted is that I cannot find a place for the DocEnd
bookmark in a two-page document (I want to print both pages) that is not
identifed as either page 1 or page 3. However, if I use the Word Count
toolbar to count the number of characters, and replace x2 with that number
(e.g. 2408), this line properly identifies p2 as 2:
p2 = rTmp.Characters(x2).Information(wdActiveEndPageNumber)

If I do not manually set x2 to 2408, it is identified as something like 4500
(which is more than the total number of characters in the document), and
that line of code throws the following error:
5941 - The expected member of the collection does not exist

That is all the information I have been able to obtain. The problem seems
to be the same whenever it occurs: the character position of the bookmarks
(the variables x1 and x2) are not identified properly.

If I could identify the page on which each of the bookmarks is located, I
could get the code to do what is needed. If this is not possible, I could
settle for setting the variables p1 and p2 manually for documents in which
problems occur, and have the code above start by testing whether the
variables already have a value, and using that value instead of the values
generated by the code, or something like that. I would rather not do that,
but I can accept a workaround if necessary.

I am using Word 2003. If it matters, the code above is designed to include
the header and footer in the printout. Other code with which I experimented
did not print the header and footer. It is better by far if I can include
the header and footer.


.



Relevant Pages

  • Re: subscript or superscript in the bookmarks.
    ... subscript or superscript will be lost in the bookmarks. ... In the APPENDIX D Character Sets and Encodings of this book. ... 1- For the same character, e.g., twosuperior, we can find that the ...
    (comp.text.tex)
  • Re: _Hlt Bookmark
    ... I tried inserting a "plain" bookmark, then a hyperlink to ... character - the character I clicked on. ... I could create _Hlt bookmarks systematically. ... but the first 5 digits in all the _Hlt bookmarks I ...
    (microsoft.public.word.docmanagement)
  • Re: subscript or superscript in the bookmarks.
    ... belong to the CJK Unified Ideographs Extension A/B into the bookmarks, ... include this character or not, I'm afraid that the answer is not. ... the display of these characters, I must use something like the inner ...
    (comp.text.tex)
  • Options|Override Default Profile
    ... Bookmarks for commonly accessed URLs may be set here. ... F7 - Open configuration window for the current window ... I love that 'Boschlooney the Last' character 32C ...
    (sci.crypt)
  • Re: List pages to print, select one page
    ... no advantage over bookmarks, even if I can get it to work. ... Dim oVar As Variables ... Private Sub UserForm_Initialize ...
    (microsoft.public.word.vba.general)

Loading