使用VBA在MS Word上的内联形状下添加一个TextBox

时间:2016-10-26 10:24:48

标签: vba ms-word word-vba

我尝试在所选图像下面插入文本框并使用内联文本换行并将其放置在图像的左下角。我使用下面的代码没有太大的成功。我不太确定是使用ShapeRange还是InlineShape。有什么指针吗?

Dim shp As Object
'Set shp = Selection.ShapeRange(1)
'Set rng = shp.Anchor
Set shp = Selection.InlineShapes(1)
Set rng = shp.Range

With ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=shp.Left, Top:=(shp.Top + shp.Height), Width:=shp.Width / 3, Height:=shp.Height / 6, Anchor:=rng)
    .TextFrame.TextRange.Font.Size = 14
    .TextFrame.TextRange.Font.Color = RGB(186, 14, 29)
    .TextFrame.TextRange.Font.Name = "Sabon MT"
    .TextFrame.TextRange = "T"
End With

1 个答案:

答案 0 :(得分:0)

我设法找到了从这里获取形状坐标的解决方案:http://www.vbaexpress.com/forum/archive/index.php/t-48831.html

这是我的代码:

Sub AddTextBox

    Set shp = Selection.InlineShapes(1)
    Set rng = shp.Range       
    Set tb = ActiveDocument.Shapes.AddTextbox(1, fcnXCoord, fcnYCoord + shp.Height, shp.Width, shp.Height / 6)

End Sub

Function fcnXCoord() As Double
    fcnXCoord = Selection.Information(wdHorizontalPositionRelativeToPage)
End Function

Function fcnYCoord() As Double
    fcnYCoord = Selection.Information(wdVerticalPositionRelativeToPage)
End Function