1

我正在尝试添加一个特别是文本框的形状。

我需要在通过 vba 添加的所有内容之后添加它。我似乎不知道该怎么做,因为添加形状需要精确测量 Left 和 Top 参数。

Dim shpActual 
Dim pos, PtsToInches 
set shpActual = Selection.Shapes.AddTextbox(msoTextOrientationHorizontal, 92, PtsToInches, 437.4, 69) 
pos = Selection.Range.Informatio(wdVerticalPositionRelativeToPage) 
PtsToInches = pos / 72
4

1 回答 1

1

.RelativeVerticalPosition插入形状后,您可以使用来定位形状。看这个例子

Sub Sample()
    Dim objShape As Shape

    Set objShape = ActiveDocument.Shapes.AddTextbox _
    (Orientation:=msoTextOrientationHorizontal, _
    Left:=10, Top:=10, Width:=80, Height:=80)

    With objShape
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
        .RelativeVerticalPosition = wdRelativeVerticalPositionBottomMarginArea
        .Left = wdShapeCenter
        .Top = wdShapeTop
    End With
End Sub

跟进

另一种方法是找到光标位置,然后在该位置插入形状。例如,这将在光标所在的位置插入一个形状。因此,在您原来的 VBA 代码中,您可以使用Selection.TypeParagraph移动到下一行,然后调用下面的代码。

Sub Sample()
    Dim objShape As Shape
    Dim pos, PtsToInches

    Set objShape = ActiveDocument.Shapes.AddTextbox _
    (Orientation:=msoTextOrientationHorizontal, _
    Left:=10, Top:=10, Width:=80, Height:=80)

    pos = Selection.Information(wdVerticalPositionRelativeToPage)

    PtsToInches = pos / 72

    With objShape
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
        .Left = wdShapeCenter
        .Top = PtsToInches
    End With
End Sub
于 2013-01-18T09:55:26.600 回答