1

我有一个包含许多矩形形状的文档。我想在完全相同的地方用一个 TextBox 替换其中的每一个。我的出发点是使用我想用文本框替换的现有已知形状(稍后我将添加进一步的自动化来处理选定的形状或所有形状)。

到目前为止,这是我的代码:

Sub Macro3()
'
' Macro3 Macro
'
'
Dim shp As Shape
Dim Box As Shape

For Each shp In ActiveDocument.Shapes.Range(Array("Group 1928"))
    shp.Select
    Set Box = ActiveDocument.Shapes.AddTextbox( _
    Orientation:=msoTextOrientationHorizontal, _
    Left:=shp.Left, Top:=shp.Top, Width:=shp.Width, Height:=shp.Height)
    Box.RelativeHorizontalPosition = shp.RelativeHorizontalPosition
    Box.RelativeVerticalPosition = shp.RelativeVerticalPosition
    Box.TextFrame.TextRange.Text = "Some text"
Next shp

End Sub

我尝试设置许多其他属性,但文本框始终出现并保持在文档页面的顶部中心。

感谢您提供的任何指导。

问候蒂姆

4

2 回答 2

1

例如:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, rt As Long, rl As Long, wf As Long, Shp As Shape
With ActiveDocument
  For i = .Shapes.Count To 1 Step -1
    With .Shapes(1)
      If .Type = msoAutoShape Then
        wf = .WrapFormat.Type
        rt = .TopRelative
        rl = .LeftRelative
        Set Shp = ActiveDocument.Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
          Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height, Anchor:=.Anchor)
        With Shp
          .WrapFormat.Type = wf
          .TopRelative = rt
          .LeftRelative = rl
          .TextFrame.TextRange.Text = "Hello World"
        End With
        .Delete
      End If
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
于 2021-01-25T20:11:52.343 回答
0

尝试这个。在我看来,您不需要创建一个 TextBox,您可以简单地更改现有的框,如下所示:

Dim shp As Shape
Dim Box As Shape

scount = ActiveDocument.Shapes.Count

For i = 1 To scount
    Set shp = ActiveDocument.Shapes(i)
    shp.TextFrame.TextRange.Text = "Some txt"
    shp.Fill.ForeColor.RGB = RGB(255, 255, 255)
    shp.Line.ForeColor.RGB = RGB(255, 255, 255)
    shp.TextFrame.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
Next i
于 2021-01-25T20:00:46.183 回答