在文档末尾添加Shape

时间:2013-01-18 09:32:08

标签: vba ms-word word-vba

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

我需要在通过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

1 个答案:

答案 0 :(得分: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