用ms-word中的不同值替换字符串的相同出现次数

时间:2018-02-24 14:46:19

标签: ms-word word-vba

我的文字有五个' XX'分散在不同的地方。我想 将第一个XX替换为1.,将第二个实例替换为2.和 所以,直到用5替换第五个实例。我试着用 以下代码没有成功:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
  .Text = "XX"
  .Replacement.Text = "1."
  .Execute Replace:=wdReplaceOne
  .Forward = True
  .Text = "XX"
  .Replacement.Text = "2."
  .Execute Replace:=wdReplaceOne
  .Forward = True
  [...]
  .Text = "XX"
  .Replacement.Text = "5."
  .Execute Replace:=wdReplaceOne
  .Wrap = wdFindStop
End With
End Sub

如何修改上述代码,或者我还应该做些什么才能获得这些代码?如果我使用" sed"在文本文件中,我只需执行:s/XX/1./1s/XX/2./2s/XX/3./3等。

3 个答案:

答案 0 :(得分:0)

您可以使用计数器作为替换文本,使用For...Next循环来递增计数器。这将节省必须编写大量重复代码,如第一个代码示例所示。

通过使用重复的Do...Until循环直到找不到搜索词的更多实例,可以使代码更加灵活,适用于任意数量的搜索词。这种变化在第二个代码示例中。

这两种变体都包含搜索整个文档或当前选择的代码。只需注释掉你不需要的那个并删除你所做的那个评论......

Sub FindAndReplaceWithNumbers()
    Dim rngDoc As word.Range, rngFind As word.Range
    Dim findString As String
    Dim counter As Long
    Dim found As Boolean

    findString = "XX"
    'To search the entire document
    'Set rngDoc = ActiveDocument.content
    'To search a selection, only
    Set rngDoc = Selection.Range
    Set rngFind = rngDoc.Duplicate
    With rngFind.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .wrap = wdFindStop
      .Forward = True
      .Text = findString
    End With
    For counter = 1 To 5
        With rngFind.Find
            .Replacement.Text = CStr(counter) & "."
            found = .Execute(Replace:=wdReplaceOne)
        End With
        rngFind.End = rngDoc.End
    Next
End Sub

Sub FindAndReplaceWithNumbers()
    Dim rngDoc As word.Range, rngFind As word.Range
    Dim findString As String
    Dim counter As Long
    Dim found As Boolean

    findString = "XX"
    counter = 1
    'To search the entire document
    'Set rngDoc = ActiveDocument.content
    'To search a selection, only
    Set rngDoc = Selection.Range
    Set rngFind = rngDoc.Duplicate
    With rngFind.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .wrap = wdFindStop
      .Forward = True
      .Text = findString
    End With
    Do
        With rngFind.Find
            .Replacement.Text = CStr(counter) & "."
            found = .Execute(Replace:=wdReplaceOne)
        End With
        rngFind.End = rngDoc.End
        counter = counter + 1
    Loop Until Not found
End Sub

答案 1 :(得分:0)

更简单,更快捷:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "XX"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .MatchWildcards = True
    .Execute
  End With
  Do While .Find.Found
    i = i + 1
    .Text = i
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " records updated."
End Sub

答案 2 :(得分:0)

要将代码的范围限制为选择,您可以使用:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, i As Long
With Selection
  Set Rng = .Range
  With .Range
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "XX"
      .Replacement.Text = ""
      .Forward = True
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Execute
    End With
    Do While .Find.Found
      If .InRange(Rng) Then
        i = i + 1
        .Text = i
      Else
        Exit Do
      End If
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
End With
Application.ScreenUpdating = True
MsgBox i & " records updated."
End Sub