View Single Post
 
Old 11-23-2013, 02:07 AM
macropod's Avatar
macropod macropod is offline Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, j As Long, k As Long, l As Long, x As Long, y As Long
Dim Rng As Range, StrFnd As String
With ActiveDocument
  StrFnd = InputBox("Please input the string to find.")
  x = InputBox("Please input the allowable string frequency.")
  y = InputBox("Please input the test paragraph interval.")
  For i = 1 To .Paragraphs.Count - 1
  k = 0: l = 0
    For j = i To .Paragraphs.Count
      Set Rng = .Range(.Paragraphs(i).Range.Start, .Paragraphs(j).Range.End)
      If Trim(.Paragraphs(j).Range.Text) <> vbCr Then k = k + 1
      If k = y Then
        With Rng.Duplicate
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = False
            .Forward = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Text = StrFnd
            .Replacement.Text = ""
            .Wrap = wdFindStop
            .Execute
          End With
          Do While .Find.Found
            If .InRange(Rng) Then
              l = l + 1
              .Collapse wdCollapseEnd
              .Find.Execute
            Else
              Exit Do
            End If
          Loop
        End With
        If l > x Then
          With Rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Format = True
            .Forward = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Replacement.Highlight = True
            .Text = StrFnd
            .Replacement.Text = "^&"
            .Wrap = wdFindStop
            .Execute Replace:=wdReplaceAll
          End With
        End If
      End If
    Next
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote