替换MS-Word中的文本框

问题描述 投票:0回答:1

我尝试了这两种方法。一个很好,但另一个没有。有人解释为什么吗?

1-这个工作了

Dim scount As Integer
Dim x As Integer
scount = WordDoc.Shapes.Count
For x = 1 To scount
WordDoc.Shapes(x).Select
If WordDoc.Shapes(x).TextFrame.HasText = True Then
With WordDoc.Shapes(x).TextFrame.TextRange.Find
  .Text = "World"
  .Replacement.Text = "505"
  .Forward = True
  .Wrap = wdFindContinue
  .Execute Replace:=wdReplaceAll
End With
End If
Next x

2-这个没有用

Dim Shp As Shape

For Each Shp In ActiveDocument.Shapes
  Shp.Select
     With Selection.Find
        .Text = "World"
        .Replacement.Text = "Here"
        .Forward = True
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
Next
vba replace ms-word textbox
1个回答
0
投票

您的第二个宏不起作用,因为它不能解决TextFrame或TextRange问​​题。这将起作用:

Dim Shp As Shape
For Each Shp In ActiveDocument.Shapes
  With Shp
    If .TextFrame.HasText = True Then
      With .TextFrame.TextRange.Find
        .Text = "World"
        .Replacement.Text = "Here"
        .Forward = True
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
      End With
    End If
  End With
Next
© www.soinside.com 2019 - 2024. All rights reserved.