Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 02-15-2017, 03:17 AM
slaycock slaycock is offline Do not add two spaces after Mr. or Ms. Windows 7 64bit Do not add two spaces after Mr. or Ms. Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Try the following as a more sophisticated approach.

Code:
Sub FixTwoSpacesAfterPeriod(Optional ByVal FindRange As Range = Nothing)

Dim myAllowedWords As String
Dim myRange As Range
Dim myRangeCopy As Range
Dim myRangeEnd As Long

    ' The literal string below could be replaced by a reference to a custom document property
    ' Which makes the list editable by the user
    ' e.g. myOneSpaceWords = ActiveDocument.CustomDocumentProperties("OneSpaceWords")
    
    myOneSpaceWords = "Dr,Ms,Mr,Mrs,Messrs,Hon"
    
    ' Check the parameter and if it is 'Nothing' check for a selected range before using the 
    ' whole document main story range
    
    If FindRange Is Nothing Then
        If Selection.Range.End <> Selection.Range.Start Then
            Set myRangeCopy = Selection.Range
        Else
            Set myRangeCopy = ActiveDocument.StoryRanges(wdMainTextStory)
        Else
    Else
        Set myRangeCopy = FindRange
    End If
    
    ResetFindReplaceParameters
    
    ' First do an undconditional Find and Replace
    
    Set myRange = myRangeCopy
    With myRange.Find
        .Text = "([.\?\!]) {1,}"
        .Replacement.Text = "\1  "
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
    
    ' Now remove the two spaces for the list of myOneSpaceWords
    ' The myrange.Select are included  to show the logic when stepping
    ' Through the code with F8.  They should be deleted for general use
    
    myRangeEnd = myRangeCopy.End
    With myRange.Find
        Do
            myRange.End = myRangeEnd
            myRange.Select
            .Text = "(?)(. {2,})"
            .MatchWildcards = True
            .Execute
            ' myRange is now the found text
            myRange.Select
            If .Found Then
                If InStr(myOneSpaceWords, myRange.Words.First) > 0 Then
                    ' delete the last space in the found text
                    myRange.Characters.Last.Delete
                    myRange.Select
                End If
            End If
            myRange.Collapse direction:=wdCollapseEnd
            myRange.Select
        Loop Until Not .Found
    End With
End Sub

Sub ResetFindReplaceParameters()

' The use of a range object is critical to the success of this macro
Dim myRange As Range
  
  Set myRange = ActiveDocument.StoryRanges(wdMainTextStory).Characters(1)
  With myRange.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop  '  This means stop when we get to the end of the range
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
End Sub
If I were using the above code I would have to add a third search and replace because I have names of micro-organisms which are usually italicised e.g. E. Coli

In which case I would have .format=true, .find.font.italic=true and .replace.font.italic

I'd then call the ResetFindReplaceParameters before the fourth search.
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do not add two spaces after Mr. or Ms. Two spaces after a period mysterytramp Word 14 11-06-2017 02:01 PM
Do not add two spaces after Mr. or Ms. Spaces in a chart AIKA Excel 3 10-27-2015 01:37 AM
Do not add two spaces after Mr. or Ms. Spaces between words, help! cheffie Word 2 10-02-2013 01:59 PM
Strange spaces Trevor_Bauer Word 1 03-08-2012 11:56 AM
Spaces After Each Word jnutella Word 0 03-04-2009 02:00 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 07:49 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