Re: Identify page on which a bookmark is located



Thanks for the reply. As it happens the ShowAll did not accomplish what was
needed (it still gave an incorrect number for the character position of the
second bookmark. However, as I investigated further I think I came up with
something. So far it seems to work in all situations.

Dim p1 As Long ' start page
Dim p2 As Long ' end page
Dim AllChar As Long ' Section character count

Dim rTmp As Range
Dim intSect As Integer

' Go to the first bookmark
Selection.GoTo What:=wdGoToBookmark, Name:="DocStart"

' Find the number of the section containing the bookmark
intSect = Selection.Information(wdActiveEndSectionNumber)
' Count the characters in the section
AllChar =
ActiveDocument.Sections(intSect).Range.ComputeStatistics(wdStatisticCharactersWithSpaces)
Set rTmp = ActiveDocument.Sections(intSect).Range
' At the first character in the section, find the page number
p1 = rTmp.Characters(1).Information(wdActiveEndPageNumber)
' At the last character in the section, find the page number
p2 = rTmp.Characters(AllChar).Information(wdActiveEndPageNumber)

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

Set rTmp = Nothing

This assumes the printout is all of one section. I expect the code could be
adapted for printing multiple (including non-contiguous) sections. I think
I can avoid those situations, but if not, I think there is a way to work
with it.

"Russ" <drsN0SPAMmikle@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C34456C7.1E624%drsN0SPAMmikle@xxxxxxxxxxxxxxxxxxxxxxxx
Bruce,
Three things may trip you up:
1.Word usually determines page count from the current default printer
driver.
2.Each section between section breaks may have different Page Setup
formatting. So you may also have to include the section numbers that your
starting bookmark and ending bookmark are in.
3.Shown Hidden text (Index fields, etc.) may alter what .Information...
may
see versus what Print Preview sees, if the option to not print hidden text
is on. If that is the case, then use the not show hidden text toggle
before
using .Information...

Dim blnShowAll As Boolean
blnShowAll = ActiveWindow.ActivePane.View.ShowAll
ActiveWindow.ActivePane.View.ShowAll = False
...most of your code
ActiveWindow.ActivePane.View.ShowAll = blnShowAll

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.



--
Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID



.



Relevant Pages

  • Re: Identify page on which a bookmark is located
    ... I tried inserting another bookmark and finding the page as I did with the ... even though the last character was clearly on the second page. ... Dim rTmp As Range ... Set rTmp = ActiveDocument.Sections.Range ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... I tried inserting another bookmark and finding the page as I did with the ... even though the last character was clearly on the second page. ... Dim rTmp As Range ... Set rTmp = ActiveDocument.Sections.Range ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... The DocStart bookmark is identified ... going to the character before the first character in the ... an insertion point, but even if it is not I expect Previous can be ... Why use adjusted page argument - wdActiveEndAdjustedPageNumber? ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... The DocStart bookmark is identified ... going to the character before the first character in the ... an insertion point, but even if it is not I expect Previous can be ... Why use adjusted page argument - wdActiveEndAdjustedPageNumber? ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... I tried inserting another bookmark and finding the page as I did with the ... even though the last character was clearly on the second page. ... Dim rTmp As Range ... Set rTmp = ActiveDocument.Sections.Range ...
    (microsoft.public.word.vba.general)