locked
Word VBA - Need to find some word within a coded text, change font color and insert a comment RRS feed

  • Question

  • Hi!

    I have to find some (whole words - Ex: "I", "we", "us", "our") within a paragraph, surrounded by a coding <ABS>...</ABS>.  Irrespective of the order, all occurrences of each word should be highlighted and a comment should be inserted ("CE: The use of personal pronouns (I, we, us, our) is not permitted in the abstract.").

    Please check the following coding:

    [code]

    Sub z_Abstract()
    Dim strFind() As String
    Dim oRng As Range
    Dim oRngTag As Range
    Dim lngIndex As Long
      strFind = Split("I|we|us|our", "|")
      Set oRng = ActiveDocument.Range
      With oRng.Find
        Do While .Execute(FindText:="\<ABS\>*\<\/ABS\>", MatchWildcards:=True)
         
          For lngIndex = 0 To UBound(strFind)
            Set oRngTag = oRng.Duplicate
            With oRngTag.Find
              .ClearFormatting
              .Replacement.ClearFormatting
              .MatchWholeWord = True
              Do While .Execute(FindText:=strFind(lngIndex), MatchWholeWord:=True, MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
                With oRngTag
                  .HighlightColorIndex = wdTurquoise
                  .Font.Color = wdColorPink
                  .Comments.Add oRngTag, "CE: The use of personal pronouns (I, we, us, our) is not permitted in the abstract."
                  .Collapse wdCollapseEnd
                  If .End >= oRngTag.End Then Exit Do
                End With
              Loop
            End With
          Next lngIndex
          oRng.Collapse wdCollapseEnd
        Loop
      End With
    lbl_Exit:
      Exit Sub
    End Sub

    [/code]

    I have tested this coding, the result is shown below:

    < ABS> This is for testing: I[a1] n, awe[a2] some, hous[a3] e, hour[a4]. The use of personal pronouns (I, we, us, our) is not permitted in the abstract. The use of personal pronouns (us, I, our, we) is not permitted in the abstract.</ABS>

    Comments are inserted for each occurrences correctly.

    My requirements:
    1. Only whole words to be find. Now, it finds part of words also.
    2. All occurrences of the words should be highlighted. Now, only one occurrence highlighted.

    Kindly help!

    Thanks
    Alex

     
    Monday, March 16, 2015 2:37 PM