问题
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