Physical width of Range or Selection.

C

chuckvorndran

I'm struggling trying to calculate the width of a text range, in real-world
units, in Word 2003 using C#. I need to get a number that I can use to set a
table column width or to find the physical beginning position, in terms of
ruler measurements, for the beginning of a right justified single line
paragraph. For instance, the value rng.Paragraphs[1].Range.End may be 15
(characters or spaces), but I really need to know how many points, cm, or
inches that really is. (Yes, the actual width of displayable text is 14.) I
know that it depends on the Font, print drivers, actual characters used, etc,
but internally Word already should have done that calculation. Maybe, I just
need to know how to get at it.

Also, how do I programmatically check for a wrapped line, i.e. one that is
physically too long to fit on a single line.

I would think that this isn't an entirely uncommon task, but I haven't seen
any documentation on this particular aspect.

Thanks.
 
D

Doug Robbins

Check out the .Information function

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
H

Helmut Weber

Hi,
so far, I haven't heard of a perfect solution,
except taking on the challenge of analyzing
all kinds of typeface information and printer drivers.
So the following would be a compromise:
Sub Test790()
' wdFirstCharacterLineNumber = 10
' more than 1 line in paragraph 3
Dim x1 As Single
Dim x2 As Single
Dim rl As Range
Set rl = ActiveDocument.Paragraphs(3).Range
x1 = rl.Information(10)
rl.End = rl.End - 1
rl.Collapse Direction:=wdCollapseEnd
x2 = rl.Information(10)
If x1 <> x2 Then
MsgBox "more then 1 line"
End If
End Sub
' -----
Sub test789()
' linelength of a 1 line paragraph
' which is paragraph 4 in this example
' wdHorizontalPositionRelativeToPage = 5
Dim x1 As Single
Dim x2 As Single
Dim rl As Range
Set rl = ActiveDocument.Paragraphs(4).Range
x1 = rl.Information(5)
rl.End = rl.End - 1
rl.Collapse Direction:=wdCollapseEnd
x2 = rl.Information(5)
MsgBox "Linelength = " & x2 - x1 & " Points"
End Sub
You may have to switch views a bit beforehand.
Otherwise wdFirstCharacterLineNumber may return -1.
 
C

chuckvorndran

Helmut and Doug,

Thank you both. After finding the keys to conversion to C#.NET, I was able
to compare x1 and x2 in a couple of while loops that adjust the LeftIndent in
increments of one point until x2 was on the threshold of indicating the next
line.

Regarding the translation from VBA's to C#.NET.
In VBA
x1 = rng.Information(wdFirstCharacterLineNumber)
in C# becomes
x1 =
Convert.ToInt32(rng.get_Information(Word.WdInformation.wdFirstCharacterLineNumber))

And the collapse statements had to take the form of
oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
rng.Collapse(ref oCollapseEnd);

In the end, everything works fine. Thanks again for your direction.

Chuck
 

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