Add Shape at the end of the document

主宰稳场 提交于 2019-12-31 04:05:19

问题


I'm trying to add a shape specifically a textbox.

I need to add it after all the contents I added through vba. I can't seem to figure how to do it since adding the shape needs the exact measurement of the Left and Top parameters.

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:


You can use the .RelativeVerticalPosition to position the shape after you insert the shape. See this example

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

Followup

One other way is to find the cursor position and then insert the shape at that position. For example this will insert a shape where the cursor is. So in your original VBA code, you can use Selection.TypeParagraph to move to the next line and then call the below code.

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


来源:https://stackoverflow.com/questions/14395929/add-shape-at-the-end-of-the-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!