converting points to centimeters...

S

Sam

Hi all

I'm having a bit of trouble with CentimetersToPoints and PointsToCentimeters.

I'm saving settings to ListTemplates and then reading the settings. The
settings save CentimetersToPoints just fine, but when I read them back
PointsToCentimeters they're not the same. For instance, if I save something
as CentimetersToPoints(1.25) the PointsToCentimeters conversion will come
back as 1.250597.

When I save InchesToPoints I get back the same setting in PointsToInches (ie
saving .NumberPosition = InchesToPoints(1.25) returns
PointsToInches(.NumberPosition) = 1.25).

We can't use inches because we're metric.

Anybody have any ideas on how to make sure that when I save something metric
as 1.25 I'll get it back as 1.25 (or whatever)? Thanks for any advice...

Here's some code to demonstrate the problem:


Sub SetupLT()

Dim lstListTemplate As ListTemplate
Dim blnLTExists As Boolean

blnLTExists = False

For Each lstListTemplate In ActiveDocument.ListTemplates
If lstListTemplate.Name = "MyLT" Then
blnLTExists = True
Exit For
End If
Next lstListTemplate

If blnLTExists = False Then
Set lstListTemplate = ActiveDocument.ListTemplates.Add _
(OutlineNumbered:=True, Name:="MyLT")
ElseIf blnLTExists = True Then
Set lstListTemplate = ActiveDocument.ListTemplates("MyLT")
End If

With lstListTemplate.ListLevels(1)
.NumberPosition = CentimetersToPoints(1.25)
.TextPosition = CentimetersToPoints(2.5)
End With

End Sub

Sub ReadLT()

Set lstListTemplate = ActiveDocument.ListTemplates("MyLT")

With lstListTemplate.ListLevels(1)
MsgBox PointsToCentimeters(.NumberPosition)
MsgBox PointsToCentimeters(.TextPosition)
End With

End Sub
 
D

Doug Robbins

Do it like this:

Dim x As Single
x = CentimetersToPoints(1.25)
MsgBox PointsToCentimeters(x)

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Sam

Hi Doug

Thanks for the reply, I'll give it a try. Can I ask what the reason is for
using a variable instead of setting the property directly?
 
S

Sam

Hi Doug

Thanks again for the suggestion. Unfortunately the Single variable doesn't
seem to help. I modified my code as below. The msgbox x still returns
1.250597. The problem seems to arise when the CentimetersToPoints value is
stored or when the setting is converted back - your code works fine but it
doesn't actually convert and store the value or convert a stored value.

Any ideas as to what's going on and how to fix it?

Sub SetupLT()

Dim lstListTemplate As ListTemplate
Dim blnLTExists As Boolean

blnLTExists = False

For Each lstListTemplate In ActiveDocument.ListTemplates
If lstListTemplate.Name = "MyLT" Then
blnLTExists = True
Exit For
End If
Next lstListTemplate

If blnLTExists = False Then
Set lstListTemplate = ActiveDocument.ListTemplates.Add _
(OutlineNumbered:=True, Name:="MyLT")
ElseIf blnLTExists = True Then
Set lstListTemplate = ActiveDocument.ListTemplates("MyLT")
End If

Dim x As Single
x = CentimetersToPoints(1.25)
With lstListTemplate.ListLevels(1)
.NumberPosition = x
.TextPosition = x
End With

End Sub

Sub ReadLT()

Set lstListTemplate = ActiveDocument.ListTemplates("MyLT")

Dim x As Single
With lstListTemplate.ListLevels(1)
x = PointsToCentimeters(.NumberPosition)
MsgBox x
End With

End Sub
 
J

Jean-Guy Marcil

Sam was telling us:
Sam nous racontait que :
Hi Doug

Thanks again for the suggestion. Unfortunately the Single variable
doesn't seem to help. I modified my code as below. The msgbox x
still returns
1.250597. The problem seems to arise when the CentimetersToPoints
value is stored or when the setting is converted back - your code
works fine but it doesn't actually convert and store the value or
convert a stored value.

Any ideas as to what's going on and how to fix it?

I can tell you what is going wrong, but I am not sure about the fixing
part..

The problem is not the conversion itself, but the act that Word rounds the
list value to the nearest 0.05 point.

x = CentimetersToPoints(1.25)

results in x being equal to 35.43307.

But,
Dim x As Single
With lstListTemplate.ListLevels(1)
x = .NumberPosition
MsgBox x
End With

yields a value of 35.45, which once converted in cm is exactly 1.250597.

If you try with
x = 35.43307
when setting up the list template you still end up with 35.45.

No matter what I tried, I either ended up with 35.4 or 35.45.

When you work with inches, it so happens that 1.25 inches is exactly 90
points (1 inch = 72 points), so no rounding up occurs.

If you do the whole thing by hand, you will only see 1.25 cm because Word
does not allow/ignores the 3rd digit after the decimal point, so you would
never see it as 1.250597.

I do not think it can be fixed.

I guess that it you round up the x in
x = PointsToCentimeters(.NumberPosition)
MsgBox x
to 2 digits after the decimal point, the result will be OK anyway... so
there isn't really a problem.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Sam

Hi Jean-Guy

Thanks for that explanation. I suppose that Word's rounding to the nearest
hundredth of a point is necessary (it can't go to an infinite number of
decimal points), and Word's unfortunate points orientation is an evil we all
have to live with.

I'm not comfortable with the rounding because it's not controllable - it's
quite possible that a user may want a specific measurement that will be
unachievable because of the points-rounding and the results-rounding. But
that's life. You can't always get what you want.

I'm going to have to do the rounding manually in the code because the
solution has to be Word 97 compatible and I don't believe there's a Round
function or method available in 97.

Thanks again.
 

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