Reputation: 1
I want to calculate the length of the line, relative to a full line, using VBA. I mean that the last line in the paragraph (when the text is justified) is not full, so I want to calculate the percent that the text fills out of a full line.
I want to calculate the physical size, not the number of characters.
I found that question here, but anyone actually answered...
Upvotes: 0
Views: 316
Reputation: 25663
This is not straight-forward in Word since a "line" is dynamic - it breaks wherever Word thinks it should when it lays out the page. Therefore, only way to determine a "line" is to use the Selection object.
Sub LengthOfLine()
Dim sel As word.Selection
Dim pgSetup As word.PageSetup
Dim iStart, iEnd As Long, dblWidth As Double
Dim dblLineLen As Double
Set pgSetup = sel.Sections(1).PageSetup
dblWidth = pgSetup.PageWidth - pgSetup.LeftMargin - pgSetup.RightMargin
Set sel = Selection
'Get to the front of the line and determine its position
sel.MoveEnd wdLine, -1
iStart = sel.Information(wdHorizontalPositionRelativeToPage)
'Get to the end of the line and determine its position
sel.MoveStart wdLine, 1
sel.MoveEnd wdCharacter, -1
iEnd = sel.Information(wdHorizontalPositionRelativeToPage)
'Calculate the length of the line
dblLineLen = PointsToCentimeters(iEnd - iStart)
Debug.Print "line length: " & dblLineLen
Debug.Print "line space remaining: " & PointsToCentimeters(dblWidth) - dblLineLen
End Sub
Upvotes: 1