#1
|
|||
|
|||
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 What type of modification is required to prevent this from happening? |
#2
|
||||
|
||||
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] |
#3
|
|||
|
|||
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? |
#4
|
|||
|
|||
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. |
#5
|
|||
|
|||
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 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. |
#6
|
||||
|
||||
Quote:
.Wrap = wdFindContinue to: .Wrap = wdFindStop 2. No, but you could assign it to a keyboard shortcut.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
||||
|
||||
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] |
#8
|
|||
|
|||
Quote:
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:
Quote:
|
#9
|
|||
|
|||
Thank you for your help, both. I will test this at work today.
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. |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Two spaces after a period | mysterytramp | Word | 14 | 11-06-2017 02:01 PM |
Spaces in a chart | AIKA | Excel | 3 | 10-27-2015 01:37 AM |
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 |