我想使用VBA计算相对于整行的行长度。我的意思是段落中的最后一行(当文本被对齐时)不满,所以我想计算文本填满整行的百分比。
我想计算物理尺寸,而不是字符数。
我发现了问题here,但实际上有人回答了......
答案 0 :(得分:0)
这在Word中并不简单,因为“行”是动态的 - 它会在Word放置页面时认为应该打破的地方。因此,只有确定“行”的方法才是使用Selection对象。
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