SpaceWidth VBA

K

kamur

Hello, I am trying to find the spacewidth between words
in a text 'Justified' word document. I have two
approaches to find the spacewidth. Both the methods gives
different results eventhough the approach seemed okay in
both cases.

In one method, I am using:

Set rng = Selection.Range
For i = 1 To rng.Characters.Count
If rng.Characters(i) = " " Then
rng.Characters(i).Select
ActiveWindow.GetPoint pLeft, pTop, pWidth,
pHeight, rng.Characters(i)
MsgBox "Width = " & pWidth
End If

In the second method, the macro calculates the x,y of
last character of the first word and then the x,y of the
first character of the next word. He then subtracts the x
values to get the space width between 2 words. But I see
this value doesnot exactly equals the value from
SpaceWidth macro....I am not sure if it should match or
not....Can you see anything wrong with this approach. His
macro is:

Sub Macro3()
'
' Macro3 Macro
' Macro recorded 9/18/2003 by Jacob
'

Selection.MoveRight Unit:=wdCharacter, Count:=1,
Extend:=wdExtend
x = Selection.Information
(wdHorizontalPositionRelativeToPage)
y = Selection.Information
(wdVerticalPositionRelativeToPage)
MsgBox (x)
MsgBox (y)
Selection.MoveRight Unit:=wdCharacter, Count:=1

x1 = Selection.Information
(wdHorizontalPositionRelativeToPage)
y1 = Selection.Information
(wdVerticalPositionRelativeToPage)
MsgBox (x1 - x)
MsgBox (y1 - y)



End Sub

Why am I getting different results on these two methods?
Shouldn't I get the same result on both? If not, what is
wrong here..

Thanks
 
H

Helmut Weber

Hi Kamur,
no matter what and how, two different methods of
measuring are likely to return different results.
The question is, whether the results of one
singular method are consistent.
The more methods you apply, the more different
results you get. It's every day practice in engineering.
Doesn't mean that any method is wrong.
Choose one way of measuring and stick to it.
;-)
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, XP, NT4.0, W98
 
K

Kumar

Greetings !!

I am still confused..If I am measuring the straight line distance
between two points, shouldn't I get same results whatever math I do?

While one method says 2.25 pixels, the second one says 4 pixels. I am
dependent on this width for my following logic. So was not sure !

Thanks
Kamur
 
L

Lars-Eric Gisslén

Kumar,

Looks like you are meassuring two different things. First you get the
bounding box for a character. The second one you meassure the distance
between two insertion points/carets. As you meassure two different things
I'm not surprised you get two different results.

I think you need to try out which method gives the best result for your
logic and stick to that method.

If we take a look at Windows API we use two different methods to draw text
(specifying a bounding box where to to draw the text) and moving the caret
(X,Y coordinates). The caret can be inserted into the end of proceding
characters bounding box or at the beginning of the preceding characters
bounding box. That means the caret possition does not have to be the same as
the characters bounding box.

Regards,
Lars-Eric
 

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