Re: Identify page on which a bookmark is located



I have not had a chance to try out your suggestions, and I am about to leave
for the day, but I want to thank you for replying. I will try implementing
your suggestions tomorrow.
BTW, I used characters because I was blundering around and stumbled upon
that, and it seemed to work. Actually, there is only one document so far
where it fails, but I wouldn't be surprised if there are others. Clearly it
is not all that reliable.

"Russ" <drsN0SPAMmikle@xxxxxxxxxxxxxxxxxxxxx> wrote in message
news:C34656F3.1E775%drsN0SPAMmikle@xxxxxxxxxxxxxxxxxxxxxxxx
Bruce,
Some quick observations.
I would use
wdActiveEndAdjustedPageNumber
For getting the end of the ranges. That automatically adjust page numbers
to
any page number manipulation going on in the document.
------------------------------------
I'm not seeing why you are involving characters?

Basically the pseudocode is:
Set a new docstart2 range = to docstart.start, if docstart is not an
insertion point and you don't want to collapse it to its start.
Set a new docend2 range = to docend.end, if docsend is not an insertion
point and you don't want to collapse it to its end.

Find wdActiveEndAdjustedPageNumber of those insertion points.
(Optional, find wdActiveEndSectionNumber of those insertion points.)

Use that information in your printout command line.




It seems I spoke too soon. In some situations the last page is not
identified correctly. I tried adding your code (ShowAll), but it had no
effect.
I tried inserting another bookmark and finding the page as I did with the
starting bookmark, but no good. It is as if the second page doesn't
exist.
To test, I deleted everything except for the 2-page section I intended to
print. The character count that I got by using the Word Count toolbar
was
the same as the one I got for AllChar using the code, but p2 in the code
was
still 1, even though the last character was clearly on the second page.
The strange part is that it usually works properly, but then on a very
similar document it doesn't work. When it doesn't work, there is nothing
to
suggest why the two similar documents behave differently.
I have found that if I add characters to the second page I can eventually
get Word to understand that the end of the section is on the second page.
Maybe I will need to add a few hundred spaces or something when I run
into a
problem, but I like to think there is something more elegant than that.

"BruceM" <bamoob@xxxxxxxxxxxxxxxx> wrote in message
news:ubcOiKkFIHA.4752@xxxxxxxxxxxxxxxxxxxxxxx
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(wdStatisticCharacter
sWithSpaces)
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






--
Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID



.



Relevant Pages

  • Re: Identify page on which a bookmark is located
    ... Dim p1 As Long ' Start page ... 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. ... Set rTmp = ActiveDocument.Sections.Range ...
    (microsoft.public.word.vba.general)
  • Re: Identify page on which a bookmark is located
    ... Dim AllChar As Long ' Section character count ... Dim rTmp As Range ... ' Go to the first bookmark ... Set rTmp = ActiveDocument.Sections.Range ...
    (microsoft.public.word.vba.general)
  • Re: Macro to print watermark
    ... bookmark somewhere other than the very beginning of the document, ... Dim x1 As Long ' start character of bookmark ... Set rTmp = ActiveDocument.Range ... I tried incorporating the watermark code, but could not get it to work ...
    (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
    ... 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)

Loading