Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

Physical width of Range or Selection.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
chuckvorndran - 15 Nov 2004 21:13 GMT
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.
Doug Robbins - 16 Nov 2004 04:34 GMT
Check out the .Information function

Signature

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

> I'm struggling trying to calculate the width of a text range, in
> real-world
[quoted text clipped - 20 lines]
>
> Thanks.
Helmut Weber - 16 Nov 2004 10:02 GMT
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.
---
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
chuckvorndran - 16 Nov 2004 19:44 GMT
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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.