Word macro improvement: Add two spaces between sentences

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Word macro improvement: Add two spaces between sentences

Post by yanlok1345 »

Hi everyone,

I have edited the macro based on Gregory K. Maxey's TwoSpacesAfterSentence

Code: Select all

Sub TwoSpacesAfterSentence()

Dim oRng As Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
  
    .ClearFormatting
    .MatchWildcards = True
    
    .Text = "(*{2})([.\!\?])([A-Z])"
    .Replacement.Text = "\1\2  \3" 'Two spaces between 2 and \
    .Execute Replace:=wdReplaceAll
    
    .Text = "(*{2})([.\!\?]) ([A-Z])"
    .Replacement.Text = "\1\2  \3" 'Two spaces between 2 and \
    .Execute Replace:=wdReplaceAll
    
    .Text = "([.\!\?]) {3,}([A-Z])"
    .Replacement.Text = "\1  \2"
    .Execute Replace:=wdReplaceAll
    
    'This should prevent most cases of improper double spacing
    'in names (e.g., F. Lee Bailey, George W. Bush, etc.)
    .Text = "([!A-Z][A-Z].)  ([A-Z])" 'Two spaces between ) and (
    .Replacement.Text = "\1 \2"
    .Execute Replace:=wdReplaceAll
    
  End With
lbl_exit:
  Exit Sub
End Sub
In a 10-page document, the macro runs smoothly when the first word of each sentence is not in bold type. However, when running the macro on a document exceeding 100 pages where the first word of the first sentence in each paragraph is bold, Microsoft Word experiences a collapse.

I tried to add "Application.ScreenUpdating = False" at the very first begining of the code and it runs a little bit faster. However, it still cannot be ran successfully in a more than 100 pages documents.

How to solve this issue? Your help will be greatly appreciated!

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: Word macro improvement: Add two spaces between sentences

Post by snb »

Code: Select all

Sub M_snb()
   For j = ThisDocument.Sentences.Count To 1 Step -1
      ThisDocument.Sentences(j).InsertAfter Space(2)
   Next
End Sub

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Word macro improvement: Add two spaces between sentences

Post by yanlok1345 »

snb wrote:
21 Oct 2023, 10:43

Code: Select all

Sub M_snb()
   For j = ThisDocument.Sentences.Count To 1 Step -1
      ThisDocument.Sentences(j).InsertAfter Space(2)
   Next
End Sub
Many thanks for your help. I tested it. It can just add two spaces after full stop and not the full stop between two sentences. And it changed the normal.dotm......

snb
4StarLounger
Posts: 586
Joined: 14 Nov 2012, 16:06

Re: Word macro improvement: Add two spaces between sentences

Post by snb »

And it changed the normal.dotm.....
That is impossible, unless you saved this macro in the normal.dot. ( In that case you should read 'VBA in Word for Dummies' before continuing).
You should always store it in the document you are working in.
It can just add two spaces after full stop and not the full stop between two sentences.
I can't say it's clear to me what you mean. Please use DeepL if necessary.

yanlok1345
StarLounger
Posts: 74
Joined: 18 Oct 2023, 14:48

Re: Word macro improvement: Add two spaces between sentences

Post by yanlok1345 »

snb wrote:
21 Oct 2023, 19:06
And it changed the normal.dotm.....
That is impossible, unless you saved this macro in the normal.dot. ( In that case you should read 'VBA in Word for Dummies' before continuing).
You should always store it in the document you are working in.
It can just add two spaces after full stop and not the full stop between two sentences.
I can't say it's clear to me what you mean. Please use DeepL if necessary.
I now choose to use Grammarly and DeepL. And you are right. I mistakenly installed it into the normal.dot. Many thanks for your help!