查找段落的边界框尺寸 (Word VBA)
Find Bounding Box dimensions for a paragraph (Word VBA)
我在 Word 2016 中使用 VBA,我想创建一个与段落大小相同的矩形(由于其他原因我无法使用边框功能)。
我可以使用这段代码获取第一个字符的位置,但是段落的底部和右端呢?
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
很遗憾,以下只是我的一厢情愿:
w = Selection.Paragraphs(1).Width
h = Selection.Paragraphs(1).Height
最后,我想执行以下操作来生成一个与段落周围的边界框大小相同的矩形:
ActiveDocument.Shapes.AddShape msoShapeRectangle, x, y, w, h
如有任何帮助,我们将不胜感激。谢谢!
当您根据您选择的段落进行思考时,您就走在了正确的轨道上。我的偏好是处理选择所指示的范围,但这是个人偏好的问题。无论如何,该段落可以分为 - 尤其是 - 第一个字符和最后一个字符。正如您已经说过的,第一个字符在页面上的位置非常靠近矩形的左上角。可以为最后一个字符建立类似的关系。以下代码可能对您有所帮助。
Private Sub TestPos()
Dim Rng As Range
Dim x As Single, y As Single
Set Rng = Selection.Range
Set Rng = Rng.Paragraphs(1).Range
With Rng
x = .Information(wdHorizontalPositionRelativeToPage)
y = .Information(wdVerticalPositionRelativeToPage)
Debug.Print x, y
.Collapse wdCollapseEnd
x = .Information(wdHorizontalPositionRelativeToPage)
y = .Information(wdVerticalPositionRelativeToPage)
Debug.Print x, y
Debug.Print .Paragraphs(1).LineSpacing
End With
End Sub
左右以段落设置的页边距为准。以下代码包含您需要的语法。
Private Sub ShowPageSetup()
Dim Rng As Range
With ActiveDocument.PageSetup
Debug.Print .LeftMargin, .RightMargin
End With
Set Rng = Selection.Range
With Rng.Paragraphs(1).Range.ParagraphFormat
Debug.Print .LeftIndent, .RightIndent
End With
End Sub
我在 Word 2016 中使用 VBA,我想创建一个与段落大小相同的矩形(由于其他原因我无法使用边框功能)。
我可以使用这段代码获取第一个字符的位置,但是段落的底部和右端呢?
x = Selection.Information(wdHorizontalPositionRelativeToPage)
y = Selection.Information(wdVerticalPositionRelativeToPage)
很遗憾,以下只是我的一厢情愿:
w = Selection.Paragraphs(1).Width
h = Selection.Paragraphs(1).Height
最后,我想执行以下操作来生成一个与段落周围的边界框大小相同的矩形:
ActiveDocument.Shapes.AddShape msoShapeRectangle, x, y, w, h
如有任何帮助,我们将不胜感激。谢谢!
当您根据您选择的段落进行思考时,您就走在了正确的轨道上。我的偏好是处理选择所指示的范围,但这是个人偏好的问题。无论如何,该段落可以分为 - 尤其是 - 第一个字符和最后一个字符。正如您已经说过的,第一个字符在页面上的位置非常靠近矩形的左上角。可以为最后一个字符建立类似的关系。以下代码可能对您有所帮助。
Private Sub TestPos()
Dim Rng As Range
Dim x As Single, y As Single
Set Rng = Selection.Range
Set Rng = Rng.Paragraphs(1).Range
With Rng
x = .Information(wdHorizontalPositionRelativeToPage)
y = .Information(wdVerticalPositionRelativeToPage)
Debug.Print x, y
.Collapse wdCollapseEnd
x = .Information(wdHorizontalPositionRelativeToPage)
y = .Information(wdVerticalPositionRelativeToPage)
Debug.Print x, y
Debug.Print .Paragraphs(1).LineSpacing
End With
End Sub
左右以段落设置的页边距为准。以下代码包含您需要的语法。
Private Sub ShowPageSetup()
Dim Rng As Range
With ActiveDocument.PageSetup
Debug.Print .LeftMargin, .RightMargin
End With
Set Rng = Selection.Range
With Rng.Paragraphs(1).Range.ParagraphFormat
Debug.Print .LeftIndent, .RightIndent
End With
End Sub