Re: Identify page on which a bookmark is located by Russ
Russ
Mon Oct 29 22:37:15 PDT 2007
Bruce says:
>I don't understand what you mean by "insertion point" in this context.
The insertion point is the thin-blinking cursor position mark where you are
typing text. Selection can be a selected word of text, a selected paragraph
of text, etc.; or it can be no text selected but only the current cursor
position or insertion point. I avoid using the term ?highlighted¹ for
selected text, because Word provides highlight colors to truly highlight
text, like a highlighter marker on paper. Selected text is text with
temporarily reversed colors for font and background to make it obvious to
the user what is selected and can be manipulated.
>Also, in my research I came cross references to ³collapsing². Because of my
lack of understanding I do not have a view one way or the other about whether I
³want to collapse it to its start².
Here is what VBA Help says about the Collapse method:
=======Quote
Collapses a range or selection to the starting or ending position. After a
range or selection is collapsed, the starting and ending points are equal.
Syntax
expression.Collapse(Direction)
expression Required. An expression that returns a Range or Selection
object.
Direction Optional Variant. The direction in which to collapse the range or
selection. Can be either of the following WdCollapseDirection constants:
wdCollapseEnd or wdCollapseStart. The default value is wdCollapseStart.
Remarks
If you use wdCollapseEnd to collapse a range that refers to an entire
paragraph, the range is located after the ending paragraph mark (the
beginning of the next paragraph). However, you can move the range back one
character by using the MoveEnd method after the range is collapsed, as shown
in the following example.
Set myRange = ActiveDocument.Paragraphs(1).Range
myRange.Collapse Direction:=wdCollapseEnd
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
======UnQuote
Collapsing may only matter if your bookmarks are not just insertion points.
If they include text, then it depends on whether you want to include the
Start and End bookmarks ³full text² in the range of pages to print?
Collapsing the current selection changes the current selection to an
insertion point, either at the beginning or end or the previously selected
text.
So in order not to include both bookmarks text, you would collapse the
Start-bookmark to its end (then find the adjusted page number) and the
End-bookmark collapsed to its start (then find the adjusted page number).
>Another thing is that I could not figure out how to use the Start property. In
my attempts to use it I got a type mismatch compile error. VBA Help
documentation is spotty at best, so I can't figure out how to resolve that (or
for that matter just how to use Start in the first place).
Here¹s one thing it says about the .Start property:
=======Quote
Returns or sets the starting character position of a selection, range, or
bookmark. Read/write Long.
=======UnQuote
Notice it says you can read or write to it, but then it says Long, which
means it is of a type number. The .Start and .End properties return a number
indicating how many characters from the document beginning, which is at
.Start = 0 and .End = 0 or ActiveDocument.Range(0,0).
Here¹s one thing it says about the .Information property:
=======Quote
Returns information about the specified selection or range. Read-only
Variant.
=======UnQuote
Notice it is read only and returns information about a type selection or a
type range input. (not a type number input, as what is returned by the
.Start property; hence your type mismatch error.)
What this boils down to is, by collapsing a range or selection to the start
or end it still remains a selection type or range type and can be used as
input to the .Information property. However you may not need to collapse to
the end because, by default, the
Selection.Information(wdActiveEndAdjustedPageNumber) returns the page number
on which the *ActiveEnd* of the selection ends.
Why use adjusted page argument - wdActiveEndAdjustedPageNumber?
Because, for instance, if the document is divided into different Word
sections with section breaks, each section can do its numbering differently,
i.e. starting the page numbers counting from the number one again. And if
your bookmarks span across different sections and you gathered the adjusted
page number and the section information with wdActiveEndSectionNumber, you
could ,for example, build up and use the string ³p2s2-p3s5² within the
.PrintOut method as shown below in the quoted Word help to print a range of
pages from Page 2(Section 2) to Page 3(Section 5).
From regular Word Help:
=========Quote
Print specific pages and sections
You can print specific pages, one or more sections, or a range of pages in
more than one section. On the File menu, click Print.
To print In the Pages box
Noncontiguous pages Type the page numbers with commas between
them. Type the range of pages with a hyphen between the starting and ending
numbers in the range. For example, to print pages 2, 4, 5, 6, and 8, type
2,4-6,8
A range of pages within a section Type p page number s section
number. For example, to print pages 5 through 7 in section 3, type p5s3-p7s3
An entire section Type s section number. For example, type s3
Noncontiguous sections Type the section numbers with commas
between them. For example, type s3,s5
A range of pages across sections Type a range of page numbers and
the sections that contain them with a hyphen between the starting and ending
numbers in the range. For example, type p2s2-p3s5
=========UnQuote
To see if you needed to build up a more ³complex² print page string, you
would use ³if statements² to test if the bookmarks were in different
sections.
Choice between using Range or Selection?
Nowadays, the preferred method is to use Ranges for speed since Selection
requires Word to graphically indicate where the current selection is located
and move it, if it changes.
Logically, the Range speed should have worked that way from the beginning.
It appeared that Word 97 had a bug in it that made Ranges slower than
Selection, but it was changed in the next version of Word to make Ranges as
speedy as they should have been.
Selection still seems to work faster in Tables than Ranges at this time.
Someone suggested it was because of the way Table structures are arranged in
memory.
Almost everything that can be defined by Selection can be defined by Range.
When you record a macro, the code usually shows Selection because manually
you must select objects to indicate what you want to apply actions upon. But
most of the time you can trim the resultant, recorded code of the
unnecessary options and change the Selections to Ranges for speed.
In your code, in order to use Ranges and not have to use the Goto Bookmark,
which Selects the Bookmark text, maybe requiring you to Collapse the
Selection; you could use Ranges like this to find out which page the
Previous Character to the First Character of the bookmark named 'End' Range
is located or the Next Character after the Last Character of the bookmark
named 'Start' Range is located:
ActiveDocument.Bookmarks("End").Range.Characters.First.Previous.Information
(wdActiveEndAdjustedPageNumber)
ActiveDocument.Bookmarks("Start").Range.Characters.Last.Next.Information
(wdActiveEndAdjustedPageNumber)
or
ActiveDocument.Bookmarks("Start").Range.Information
(wdActiveEndAdjustedPageNumber)
Since, by default, .Information (wdActiveEndAdjustedPageNumber) gives you
where the end of the range or selection is located.
>> Thanks again for hanging in there on this issue. After posting (but before
>> receiving your reply) I tried wdActiveEndAdjustedPageNumber, but it did not
>> change the outcome on that one document that was giving me trouble.
>> However, as I become more familiar with the options I found something else
>> that seems to work:
>>
>> Dim p1 As Long ' Start page
>> Dim p2 As Long ' End page
>>
>> Selection.GoTo what:=wdGoToBookmark, Name:="DocEnd"
>> p2 = Selection.Information(wdActiveEndAdjustedPageNumber)
>> Selection.GoTo what:=wdGoToBookmark, Name:="DocStart"
>> p1 = Selection.Information(wdActiveEndAdjustedPageNumber)
>>
>> ActiveDocument.PrintOut _
>> Range:=wdPrintFromTo, From:=CStr(p1), To:=CStr(p2)
>>
>> Set rTmp = Nothing
>>
>> I don't understand what you mean by "insertion point" in this context.
>> Also, in my research I came across references to "collapsing". Because of
>> my lack of understanding I do not have a view one way or the other about
>> whether I "want to collapse it to its start". Another thing is that I could
>> not figure out how to use the Start property. In my attempts to use it I
>> got a type mismatch compile error. VBA Help documentation is spotty at
>> best, so I can't figure out how to resolve that (or for that matter just how
>> to use Start in the first place).
>>
>> Any further insights (or links to explanations) would be appreciated, but in
>> particular does it seem that the code above will address the problem?
>> Again, I may want to print just page 1, or page 1 and 2, or page 7, or other
>> combinations.
>> "Russ" <drsN0SPAMmikle@hotmailD0Tcom.INVALID> wrote in message
>> news:C34656F3.1E775%drsN0SPAMmikle@hotmailD0Tcom.INVALID...
>>> 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.
>>>
>>>
>>>
>>>
>>&g