page width in points seems off (Word 2010)

KenWit

Board Regular
Joined
Nov 21, 2011
Messages
68
Hi,
I'm using Selection.Range.Information(wdHorizontalPositionRelativeToTextBoundary) to calculate the width of a line in a 2010 Word document.
A line in a 8.5 X 11" document measures 457.9 across from the left to right margins if I place the IP between the second to last character and the last. (If I put the IP after the last it returns 0.

6.5 x 72 = 468 so I guess the measurement is in points, but off by a few, even including the last letter (5.0+points).

Oddly when I tally all the character widths (of a string of numbers without blank spaces) in a 6.5" line using an array using the code below I get only 190 as a total. Am I doing something wrong?

Dim ary() As Variant
For x = 1 To 200
ReDim Preserve ary(x)
V = Selection.Range.Information(wdHorizontalPositionRelativeToTextBoundary)
Selection.MoveRight wdCharacter, 1, wdMove
w = Selection.Range.Information(wdHorizontalPositionRelativeToTextBoundary)
If w - V = 0 Then GoTo there
ary(x) = w - V
Next
there:
Selection.EndKey wdStory
For T = 1 To UBound(ary())
nb = nb + ary(T)
Next
Selection.TypeText (nb) 'nb = 190 ?
 
Last edited:

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
An 8.5in wide page is 8.5*72 = 612pt. What margins are you using? Your post suggests 1in, but your line-width suggests 1.07in. Of course, if there's a mismatch between your page size and the paper size in your printer, Word might be doing some scaling to get everything to fit if you have the 'Scale content for A4 or 8.5 x 11" paper sizes' option checked. Also, paragraph justification, tabs and repeated spaces, especially, can mess around with the layout, so the results returned by .Information(wdHorizontalPositionRelativeToTextBoundary) might not be what you'd otherwise expect.

Perhaps you could explain what you're trying to achieve.
 
Upvote 0
Thanks for the feedback.

Typography. I use Calibri, which is fine font, but the spacing between characters is a bit off to me. (E.g. The exclamation points are set off by a lot of space. The letter "a" too.) So I adjust the spacing with a userform command button. (Selection.Font.Spacing = Selection.Font.Spacing + 0.1/ - 0.1). I'd like to automate this process more. Horizontal position might help or at least streamline my code.

In terms of position, how does Word calculate that? For instance, take the following word:

Jane

If I get the position of the IP after the a and then the position of the IP before it does Word include the space between the characters (the a and the n and the a and the J? I guess that might depend on where the IP actually is, if the spacing between letters is factored in. Over the course of an entire line of text that bit of spacing adds up. Maybe that accounts for the discrepancy? My margins are set at 1 inch and the paper size is 8.5 x 11 inches. Today, I seem to be getting a better tally. 460 across. Which seems right as the space at the end isn't large enough to include the last number in the string. (Interestingly, when I condense the spacing by .1 the above code quits working and returns 0 for the tally, which is a problem. Always something :)

8888888888888888888888888888888888888888888888888888888888888888888
8
 
Upvote 0
OK, but reformatting the spacing of individual characters is liable to create maintenance problems down the track and is liable to lead to document corruption. IMHO you'd do better to simply use another font with the characteristics you desire. For example, you might use Franklin Gothic Book, a font whose characteristics are quite like Calibri, but with less white space around the !, for example.

FWIW, the following macro calculates the character widths in points (to 3 decimal places) for the standard character set in 10pt Times New Roman (you can substitute other sizes & fonts – but be aware that you won’t get meaningful results when line-wraps occur – it’s probably safer to multiply the macro’s (r - l) calculation by whatever the difference is between 10pt and the point size you want results for, such as (r - l)*1.2 for a 12pt font result). The widths are output as a table. As coded, the macro does not return sizes for ASCII values less than 32 (some of which don’t have a meaningful width). The macro calculates the character widths based on the length of strings of 144 consecutive instances of the same character (other string lengths appear to not calculate with sufficient precision).
Code:
Sub GetChrWidths()
Application.ScreenUpdating = False
Dim i As Long, l As Double, r As Double, x As Long, n As Long, StrTmp As String, StrChr As String
n = 144
With ActiveDocument
  With .PageSetup
    .PageWidth = CentimetersToPoints(55.88)
    .LeftMargin = CentimetersToPoints(0)
    .RightMargin = CentimetersToPoints(0)
    .TopMargin = CentimetersToPoints(0)
    .BottomMargin = CentimetersToPoints(0)
  End With
  With .Range
    .ParagraphFormat.Alignment = wdAlignParagraphLeft
    With .Font
      .Size = 10
      .Name = "Times New Roman"
    End With
    For x = 32 To 255
      With .Paragraphs.Last.Range
        .InsertAfter vbCr
        l = .Characters.Last.Information(wdHorizontalPositionRelativeToTextBoundary)
        StrTmp = ""
        For i = 1 To n
          StrTmp = StrTmp & Chr(x)
        Next
        .InsertAfter StrTmp
        r = .Characters.Last.Information(wdHorizontalPositionRelativeToTextBoundary)
        StrChr = StrChr & Chr(x) & vbTab & Format((r - l) / n, "0.000") & vbTab
        .Text = vbNullString
        DoEvents
      End With
    Next
    .InsertAfter Left(StrChr, Len(StrChr) - 1)
  End With
  With .PageSetup
    .PaperSize = wdPaperA4
    .Orientation = wdOrientLandscape
    .LeftMargin = CentimetersToPoints(2.5)
    .RightMargin = CentimetersToPoints(2.5)
    .TopMargin = CentimetersToPoints(2.5)
    .BottomMargin = CentimetersToPoints(2.5)
  End With
  .Range.Paragraphs.Last.Range.ConvertToTable Separator:=vbTab, Numrows:=16, NumColumns:=28
  With .Tables(.Tables.Count)
    .LeftPadding = 0
    .RightPadding = 0
    .TopPadding = 0
    .BottomPadding = 0
    .AllowAutoFit = True
    With .Range.ParagraphFormat
      .SpaceAfter = 3
      .SpaceBefore = 3
      .Alignment = wdAlignParagraphCenter
    End With
  End With
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks. Franklin Gothic Medium (I don't have Book) is tighter like you say, particularly the W's. When bumped up 1/2 a pica it's pretty close to Calibri in terms of length across a line too. Still could use some tweaking though in terms of spacing, so I investigated font editors and found FontCreator 9.0. Maybe I'll try to give that a go. A lot of learning involved, as it seems, and I'm not sure I will be able to export the tweaked font to Word 2010 if I manage to tweak Calibri and save it somehow. Doing things that way would probably be the better way to go I guess when taking into account the issues that arise when you space individual characters with the font in Word. What looks okay on the screen may also not be so when printed out. Also much less time consuming than going over a document character by character.

Thanks for the code. I'll experiment around with it today :)
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top