Results 1 to 5 of 5
  1. #1
    New Lounger
    Join Date
    Dec 2009
    Location
    Minneapolis, MN
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts

    I need Search and Replace Help

    When I type I always end a sentence with a period and a single space. However, our agency's standard is a period and two spaces. So at the end of completing my letter I want a search and replace that would [sometimes] change the single space after the period to two spaces. [Of course it can't be a global replace because there must still be only 1 space after Mr., Ms. or certain abbreviations such as a.m.]
    I thought I would use
    Find: ". ^?"
    Replace ". ^?"
    but ^? isn't allowed. Oh, I won't tell you all the things I tried and failed. What should I use.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Word VBA has a native object called a sentence, but when I ask for a count of sentences in the following, I get 4:

    Go. Mr. Smith. Go.

    So that's not very promising... perhaps if I replace the space after Mr. with a non-breaking space... no, that doesn't help.

    I think at this point I would search the web for ideas. :grin:

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,853
    Thanks
    4
    Thanked 259 Times in 239 Posts
    You can do this with a macro if you preset your grammar checking to flag sentences that don't end with double space. This appears to be clever enough to avoid double spacing non sentences like "Mr. George W. Bush and his friends"
    Code:
    Sub SentencesDoubleSpaced()
      'Check your document and double space any sentences with a single space after it
      Dim aGram As Range
      'Requires you to preset your grammar checking options to flag sentences ending with a single space
      'In Word 2010 this is in
      '  File>Proofing>When correcting spelling and grammar in Word>Settings>Require>Spaces required between sentences = 2
      ' Also remember to turn on grammar checking
      For Each aGram In ActiveDocument.GrammaticalErrors
        If Right(aGram.Text, 2) = ". " Then
          aGram.Text = aGram.Text & " "
        End If
      Next aGram
    End Sub
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. The Following 2 Users Say Thank You to Andrew Lockton For This Useful Post:

    JSwanson (2011-02-17),PamCaswell (2011-02-16)

  5. #4
    New Lounger
    Join Date
    Dec 2009
    Location
    Minneapolis, MN
    Posts
    22
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I think this works:

    Find= (. )([A-Z])
    Replace= \1 \2
    with Wildcards on

  6. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    J,

    Here's another alternative using a combination of Regular Expressions and Macros.
    The macro uses 3 Search & Replace operations as follows.
    1. Replace periods following salutations {Dr. Mr. Mrs.) with a grave accent `.
    2. Replace single spaces at end of sentences {defined as a period, space, Capital Letter.} with 2 spaces.
    3. Replace grave accent w/period.
    Code:
    Sub DSAfterSentence()
    
        With Selection.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "<([DM][rs]{1,2})(.)"  'Replace . after salutations with grave accent {`}
            .Replacement.Text = "\1`"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
        
        With Selection.Find
            .Text = "(. )([A-Z])"
            .Replacement.Text = "\1 \2" 'Replace single space w/2 spaces at end of sentence
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
        
        With Selection.Find
            .Text = "(` )"
            .Replacement.Text = ". " 'Replace grave accent w/period.
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = True
            .Execute Replace:=wdReplaceAll
        End With
        
    End Sub
    References:
    One
    Two
    Good Luck.
    Last edited by RetiredGeek; 2011-02-17 at 18:31.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. The Following User Says Thank You to RetiredGeek For This Useful Post:

    JSwanson (2011-02-18)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •