Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2016, 04:11 PM
jeffreybrown jeffreybrown is offline Do not add two spaces after Mr. or Ms. Windows Vista Do not add two spaces after Mr. or Ms. Office 2007
Expert
Do not add two spaces after Mr. or Ms.
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default Do not add two spaces after Mr. or Ms.

I'm currently using this script to add two spaces after a period.



Code:
Sub FixTwoSpacesAfterPeriod()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "([.\?\!]) {1,}"
        .Replacement.Text = "\1  "
        .Execute Replace:=wdReplaceAll
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWildcards = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
This script also add two periods after Mr. and Ms.

What type of modification is required to prevent this from happening?
Reply With Quote
  #2  
Old 04-23-2016, 09:10 PM
macropod's Avatar
macropod macropod is offline Do not add two spaces after Mr. or Ms. Windows 7 64bit Do not add two spaces after Mr. or Ms. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

It's really not feasible to do as you ask. Besides, there are far more cases than just Mr. and Ms. you'd need for consider, including abbreviations such as: 'Dr.' 'Mrs.', 'i.e.'; 'e.g.'; and 'etc.', the last of which may or may not occur at the end of a sentence. IMHO, though, the whole idea of having two spaces after each sentence is archaic; it harks back to the days of typewriters with fixed-pitch fonts and has no place in a modern document using proportional spacing and/or justification.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-14-2017, 03:06 PM
jeffreybrown jeffreybrown is offline Do not add two spaces after Mr. or Ms. Windows Vista Do not add two spaces after Mr. or Ms. Office 2007
Expert
Do not add two spaces after Mr. or Ms.
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Hi Paul,

Two questions:

First, using the macro above, how can it be modified to execute on only the selected text?
Second, can it be executed thru a right click event?
Reply With Quote
  #4  
Old 02-14-2017, 08:17 PM
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: 256
slaycock is on a distinguished road
Default

As a trial try adding ([!rs]) as the first field in your find text. Don't forget to add \2 to your replace text. Be prepared to have unexpected side effects.

Edited four times to remove stupidity.
Reply With Quote
  #5  
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: 256
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
  #6  
Old 02-15-2017, 04:42 AM
macropod's Avatar
macropod macropod is offline Do not add two spaces after Mr. or Ms. Windows 7 64bit Do not add two spaces after Mr. or Ms. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by jeffreybrown View Post
Two questions:

First, using the macro above, how can it be modified to execute on only the selected text?
Second, can it be executed thru a right click event?
1. For the original macro, change:
.Wrap = wdFindContinue
to:
.Wrap = wdFindStop
2. No, but you could assign it to a keyboard shortcut.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 02-15-2017, 04:48 AM
macropod's Avatar
macropod macropod is offline Do not add two spaces after Mr. or Ms. Windows 7 64bit Do not add two spaces after Mr. or Ms. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by slaycock View Post
Try the following as a more sophisticated approach.
More sophisticated, maybe, but:
1. It lack the code to call it - it can't run on its own; and
2. Your loop with its repeated selections (myRange.Select) is very inefficient - you should be able to do the lot without selecting anything (except for whatever is selected when the calling macro is run). There are plenty of examples of such code on this forum.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 02-15-2017, 05:28 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: 256
slaycock is on a distinguished road
Default

Quote:
More sophisticated, maybe, but:
1. It lack the code to call it - it can't run on its own; and

Mea culpa. If the OP can't work out how to use the macro it is easy to post en example of how to do so.

Quote:
2. Your loop with its repeated selections (myRange.Select) is very inefficient - you should be able to do the lot without selecting anything (except for whatever is selected when the calling macro is run). There are plenty of examples of such code on this forum.
Agreed. I've learned a heck of a lot by looking at your code examples. Hence

Quote:
' The myrange.Select are included to show the logic when stepping
' Through the code with F8. They should be deleted for general use
Which admittedly occurs halfway through the macro so might easily be overlooked.
Reply With Quote
  #9  
Old 02-15-2017, 05:39 AM
jeffreybrown jeffreybrown is offline Do not add two spaces after Mr. or Ms. Windows Vista Do not add two spaces after Mr. or Ms. Office 2007
Expert
Do not add two spaces after Mr. or Ms.
 
Join Date: Apr 2016
Posts: 673
jeffreybrown has a spectacular aura aboutjeffreybrown has a spectacular aura about
Default

Thank you for your help, both. I will test this at work today.

Quote:
Originally Posted by macropod View Post
2. No, but you could assign it to a keyboard shortcut.
Paul, usually when assigning the shortcut key it is done via, Alt + F8, select the macro, Options and assign the shortcut key.

Since this doc will be used by many and on many different computers, is it possible to carry the assigning of the shortcut key in the macro itself?

It seems I've searched for this before and the answer was the shortcut key has to be set on each individual machine.
Reply With Quote
Reply

Thread Tools
Display Modes


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 03:45 PM.


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