View Single Post
 
Old 02-28-2014, 06:42 PM
macropod's Avatar
macropod macropod is online now Windows 7 32bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,359
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

You could use something like:
Code:
Sub Demo()
Application.ScreenUpdating = False
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
Dim RngTag As Range, ArrFnd, i As Long
ArrFnd = Array("must", "shall")
For i = 0 To UBound(ArrFnd)
  With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ArrFnd(i)
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    End With
    Do While .Find.Found
      Set RngTag = .Duplicate
      With RngTag
        .End = .Sentences.First.End - 1
        While (.Characters.Last = Chr(19)) Or (.Characters.Last = Chr(23))
          .End = .End - 1
        Wend
        .Collapse wdCollapseEnd
      End With
      .Fields.Add RngTag, wdFieldEmpty, "SEQ R \# '[R'000']'", False
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
ArrFnd = Array("may", "should")
For i = 0 To UBound(ArrFnd)
  With ActiveDocument.Range
    With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ArrFnd(i)
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
    End With
    Do While .Find.Found
      Set RngTag = .Duplicate
      With RngTag
        .End = .Sentences.First.End - 1
        While (.Characters.Last = Chr(19)) Or (.Characters.Last = Chr(23))
          .End = .End - 1
        Wend
        .Collapse wdCollapseEnd
      End With
      .Fields.Add RngTag, wdFieldEmpty, "SEQ M \# '[M'000']'", False
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next
ActiveDocument.Fields.Update
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
Application.ScreenUpdating = True
End Sub
However, do note that what VBA counts as sentences, differs from grammatical sentences. For example, consider the following:

Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.

For you and me, that would probably count as one sentence; for VBA it counts as 5...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote