Points returned by Information(wdHorizontalPosition...)

  • Thread starter Charlie''s Word VBA questions
  • Start date
C

Charlie''s Word VBA questions

In a macro, the value returned by
"Selection.Information(wdHorizontalPosition..." and
"Selection.Information(wdVerticalPosition...)" is in points, even though the
help says the value is in twips. I assume that the function is accurate.
Should the returned value be rounded or truncated or used exactly as
returned? For example, if a paragraph is indented 0.5 inches, the returned
value is 36.0 points which computes to an actual indent of 0.5 inches.
However, if the paragraph is indented 0.25 inches, the returned value is
18.45 points which computes to exactly 0.25625 inches.
Can Word actually do its metrics in fractions of points and, for that
matter, in fractions of twips? When returned values are converted to twips,
occassionally the answer is in fractions of twips. I have double- and
triple-checked the calculations and for multiples of 1/2 inch (0.0, 0.5, 1.0,
etc.), the results are always perfect.
Thanks for your help.
 
J

Jezebel

You've misread the Help file. Information() returns points, not twips ...

wdHorizontalPositionRelativeToPage Returns the horizontal position of the
specified selection or range; this is the distance from the left edge of the
selection or range to the left edge of the page measured in points (1 point
= 20 twips, 72 points = 1 inch). If the selection or range isn't within the
screen area, returns - 1.

wdVerticalPositionRelativeToPage Returns the vertical position of the
selection or range; this is the distance from the top edge of the selection
to the top edge of the page measured in points (1 point = 20 twips, 72
points = 1 inch). If the selection isn't visible in the document window,
returns - 1.


Separately, I think there must be something interfering with your
positioning, rather than internal conversion problems. I normally work in
millimetres (so these measurements are never exact); but I just tried
changing to inches and setting the margin to 0.25 inches.
selection.Information(wdHorizontalPositionRelativeToPage) returns exactly
18, as expected.





"Charlie''s Word VBA questions"
 
C

Charlie''s Word VBA questions

Yes, I misread the Help. Thank you.
I did some more experimenting with this. I would bet that in your test, the
zoom was 100%. If you change the zoom to, say, 90%, the number of points
will be 18.45 in the paragraph is indented .25 inches.
 
J

Jezebel

You're right that Selection.Information() is affected by the zoom. I haven't
tested it in detail, but a rudimentary test shows different results.
However, that also suggests a workaround:

Selection.RANGE.Information() is *not* (it appears, again on a quick test)
affected by zoom.






"Charlie''s Word VBA questions"
 
C

Charlie''s Word VBA questions

Thank you. I had a problem with using Range, perhaps because I didn't use it
in all places I needed to. The macro I have gets the distance from the paper
left, the left margin, top of page, from top margin, from right margin, etc.
My macro executes nearly instantly, so I have used the following code. I
also use this method in other macros to get the user's settings, then restore
them when the macro ends.
'Zoom percent
CurrZoom = ActiveDocument.ActiveWindow.View.Zoom
ActiveDocument.ActiveWindow.View.Zoom = 100
 
J

Jezebel

You'd do better learning to use Range objects. Almost all code runs quicker
and more reliably.




"Charlie''s Word VBA questions"
 
A

Adri Rietjens

I'm having problems using
selection.information(wdVerticalPositionRelativeToPage) too. I'm using Word
2003.
Zoom is correct. Pagelayout is active. But still.
It fails when the paragraph has been formatted with 'Space before'. Only the
first line returns a correct value for
selection.information(wdVerticalPositionRelativeToPage). The second and
following lines of the paragraph return incorrect values. I use the values to
draw a horizontal line straight behind the last character of that textline.
What am I doing wrong?
 
J

Jonathan West

Adri Rietjens said:
I'm having problems using
selection.information(wdVerticalPositionRelativeToPage) too. I'm using
Word
2003.
Zoom is correct. Pagelayout is active. But still.
It fails when the paragraph has been formatted with 'Space before'. Only
the
first line returns a correct value for
selection.information(wdVerticalPositionRelativeToPage). The second and
following lines of the paragraph return incorrect values. I use the values
to
draw a horizontal line straight behind the last character of that
textline.
What am I doing wrong?

Imagine the range is actually selected and highlighted. The
wdVerticalPositionRelativeToPage value returns the position of the top of
the selection. In the first line of a paragraph with Space Before set, in
order to get the position of the top of the actual text, you need to add the
SpaceBefore value to the wdVerticalPositionRelativeToPage value.

However, it can get a bit more complex still. In the Tools Options
Compatibility dialog, if you have the "Don't use HTML paragraph spacing" box
unchecked, and your paragraphs have both Space Before and Space After set to
some value greater than zero, then the interparagraph spacing isn't the sum
of the Space After of the paragraph above and the Space Before of the
paragraph below. Instead, the inter-paragraph spacing is whichever is larger
of the two.

Now, this affects how much additional space above the first line of a
paragraph is "selected". Extra space above the first line is only selected
if the Space Before is greater than the Space After, and the amount selected
is equal to the difference in that case.

Your calculations using wdVerticalPositionRelativeToPage have to take these
issues into account.
 
C

Charlie''s Word VBA questions

I have not tried to account for each line in a paragraph. However, can you
determine the amount of "space before" and add that to the value? Or, can
you go just add the distance from line to line based on the font size and
line spacing and add that to the value of the first line? (I am not sure if
you are trying to add a line after only the last line or each line in the
paragraph.)
 
A

Adri Rietjens

To both Jonathan West and Charlie's Word VBA Questions:

Thanks a lot for your remarks/tips.


Hereby a simple version of the code I use:

Sub DrawLinesAtEndOfEachLine()

'Send cursor to first line of document (mainstory)
Selection.HomeKey unit:=wdStory, Extend:=wdMove

While Selection.InRange(ActiveDocument.Bookmarks("\Endofdoc").Range) = False
'Send cursor to last position of current line
Selection.EndKey unit:=wdLine, Extend:=wdMove

'Calculate coördinates start (pos. cursor) and end of line (right margin)
XStart = Selection.Information(wdHorizontalPositionRelativeToPage)
XEnd = ActiveDocument.PageSetup.PageWidth -
ActiveDocument.PageSetup.RightMargin
Y = Selection.Information(wdVerticalPositionRelativeToPage) + 0.5 *
Selection.ParagraphFormat.LineSpacing

'Draw line
Set NewLine = ActiveDocument.Shapes.AddLine(XStart, Y, XEnd, Y)
With NewLine.Line
.DashStyle = msoLineSolid
.Weight = 0.5
.Style = msoLineSingle
End With
'Send cursor to startposition of next line
Selection.MoveDown unit:=wdLine, Count:=1, Extend:=wdMove
Selection.EndKey unit:=wdLine, Extend:=wdMove
Wend

End Sub

Using the Range object causes much more troubles calculating the
startposition of the lines to be drawn.
Executing the above code in a document without using 'space before' causes
each line of text to be 'completed' by a line.
The built in bookmark causes the code to stop when the cursor reaches the
end of the document.

Does any of you know how to adapt above code to solve the problem (and...if
possible, to speed it up)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top