使用vba循环整个单词2007文档

时间:2011-02-01 09:08:09

标签: vba ms-word

我正在处理一个用户表单,用于查找特定短语并根据特定条件对其进行评论。我无法在文档中为所有找到的短语添加注释。它只会更改第一个找到的短语,尽管它会选择所有出现的短语。如何修改整个内容的代码?

这是我的代码:

 If Criteria2 <> "" Then
        Selection.Find.ClearFormatting
        With Selection.Find
            .Text = Criteria2
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindAsk
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
        End With
       On Error Resume Next
        With Selection
            .Comments.Add Range:=Selection.Range, Text:="SPE 2"
        End With
End If

Ok以下是新代码,但预期无效:

Selection.Find.ClearFormatting
        With Selection.Find
           .Text = CritArray(i)
           .Replacement.Text = ""
           .Forward = True
           .Wrap = wdFindContinue
           .Format = False
           .MatchCase = False
           .MatchWholeWord = False
           .MatchWildcards = False
           .MatchSoundsLike = False
           .MatchAllWordForms = False
           Do
               .Execute
               If Not .Found Then
                Exit Do
               ElseIf .Found Then
                FoundCount = FoundCount + 1
                Selection.Comments.Add Range:=Selection.Range, Text:=MessArray(i) & CritArray(i) & "' - found for the" & Str(FoundCount) & ". time"
               End If
            Loop 
        End With

使用此作为输入时得到的结果:

Testrow1
Testrow2

如下:

Testrow1 ....................................'Testrow1' - found for the 1. time
Testrow2 ....................................'Testrow2' - found for the 2. time
                                             'Testrow2' - found for the 1. time

我无法理解为什么会发生这种情况,因为如果找不到任何内容,do ..循环应该退出。有可能.Wrap = wdFindContinue是问题吗?这里有三种可能性:

  • wdFindAsk ...在文档的末尾请求在开头再次搜索(不要这样)
  • wdFindContinue ...不经询问就进行搜索
  • wdFindStop ...首次找到搜索短语时停止(不想这样)

有人知道吗?

2 个答案:

答案 0 :(得分:2)

您只需将其更改为:

.Execute Replace:=wdReplaceAll

虽然刚刚再次查看了你的答案(对不起!),你是否想要为每次更改的事件添加注释?至于此,你必须用

循环遍历每一个
Do
    ' .Execute Replace:=wdReplaceOne if you want to loop AND replace
    .Execute
    If Not .Found Then Exit Do
    Selection.Comments.Add Range:=Selection.Range, Text:="SPE 2"
Loop Until Not .Found

添加评论,直到找到/替换所有评论。

答案 1 :(得分:0)

我现在使用完全相同的循环并且它有效。新代码分别是旧代码:

For i = 0 To UBound(CritArray)
    With Selection
    .HomeKey wdStory
        With .Find
        .ClearFormatting
            Do While .Execute(FindText:=CritArray(i), _
            Forward:=True)

                Select Case i
                    Case 0: FoundCountC1 = FoundCountC1 + 1
                    Case 1: FoundCountC2 = FoundCountC2 + 1
                    Case 2: FoundCountC3 = FoundCountC3 + 1
                    Case 3: FoundCountC4 = FoundCountC4 + 1
                    Case 4: FoundCountC5 = FoundCountC5 + 1
                    Case 5: FoundCountC6 = FoundCountC6 + 1
                    Case 6: FoundCountC7 = FoundCountC7 + 1
                    Case 7: FoundCountC8 = FoundCountC8 + 1
                    Case 8: FoundCountC9 = FoundCountC9 + 1
                End Select

            Loop
        End With
    End With
Next