Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 02-28-2014, 06:42 PM
macropod's Avatar
macropod macropod is offline Append text to a sentence containing specific word Windows 7 32bit Append text to a sentence containing specific word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,521
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Word table - how do I append % symbol to each value in columns 3 & 4 of a table Dawsie Word 4 03-06-2013 12:33 AM
Append text to a sentence containing specific word Insert text at the end of a sentence Find/Replace AlmostFriday Word 6 06-17-2012 05:21 AM
Append text to a sentence containing specific word Deleteing specific text in word document ubns Word 1 05-31-2012 10:38 PM
Append text to a sentence containing specific word Change on 1 sentence applied to the whole text flexible Word 2 05-03-2012 02:25 AM
Append text to a cell with color gvdm Excel 1 04-17-2012 10:21 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:00 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft