如何将Word中的形状移动到书签位置

时间:2012-11-16 18:27:20

标签: vba ms-word

我在Word文档中有一个形状,我需要移动到书签位置。

我尝试使用" left" " top"但是,这不起作用,因为据我所知,书签没有" left"和"正确的属性。

我尝试使用剪切和粘贴,但这不适用于形状。

以下是创建形状的代码:

Set shp = ActiveDocument.Content.InlineShapes.AddOLEControl("Forms.CommandButton.1")

With ActiveDocument.InlineShapes(1).OLEFormat.Object
    .Caption = "Test"
    .Height = 30
    .Width = 44
End With

With ActiveDocument.InlineShapes(1).ConvertToShape
    .Name = "Test1"
    .ZOrder (msoBringInFrontOfText)
End With

2 个答案:

答案 0 :(得分:1)

您可以在某处使用文档上的绝对位置而不是使用书签吗?

        Dim Test1 As Shape

        Set Test1 = ActiveDocument.Shapes("Test1")

        With Test1
                .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
                .RelativeVerticalPosition = wdRelativeVerticalPositionPage
                .Left = InchesToPoints(6.889)
                .Top = InchesToPoints(0.374)
        End With


        End Sub

答案 1 :(得分:0)

这是一个非常古老的主题,但基本前提仍然有效,并且可以使用VBA在书签位置(此处为名为' bmShape')插入控件,如下所示。

Dim oRng As Range
Dim oShp As InlineShape
Set oRng = ActiveDocument.Bookmarks("bmShape").Range
oRng.Text = ""
Set oShp = oRng.InlineShapes.AddOLEControl("Forms.CommandButton.1")
oRng.End = oRng.End + 1
oRng.Bookmarks.Add "bmShape"

With oShp.OLEFormat.Object
    .Caption = "Test"
    .Height = 30
    .Width = 44
End With

With oShp.ConvertToShape
    .Name = "Test1"
    .WrapFormat.Type = wdWrapSquare
    .WrapFormat.Side = wdWrapBoth
End With