How to add space after period using macro?

A

Anne J

For example: I have a bear.He is nice.

How do I add a space between "." and "He"? Assuming that it is only applicable to ^$ (letters). Which means that if there is a number after a dot ".", the rule won't apply.

This is for Word 2003.

Please help me if someone knows. Thank you so much.
 
E

Ed Weber

Try this:

Sub SpaceAfterPeriod()
'Inserts a space after the period when the text is $.$ where $ represents
any letter.
'Does not insert the space, if a space already exists as in $. $
'Does not insert a space if a number precedes or follows the period.
ActiveDocument.Range.Find.Execute Findtext:="([A-Za-z].)([A-Za-z])", _
MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _
Replace:=wdReplaceAll
End Sub

--


Anne J said:
For example: I have a bear.He is nice.

How do I add a space between "." and "He"? Assuming that it is only
applicable to ^$ (letters). Which means that if there is a number after a
dot ".", the rule won't apply.

This is for Word 2003.

Please help me if someone knows. Thank you so much.




---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 120229-0, 02/29/2012
Tested on: 2/29/2012 11:23:45 AM
avast! - copyright (c) 1988-2012 AVAST Software.
http://www.avast.com
 
A

Anne J

I am sorry, but I can't get this code to work..

It's all mixed up with html, I copied/pasted it but it simply won't work..
 
A

Anne J

I have Word 2003. I copied all the text you posted in the VBA field, but it all turned red and doesn't work.. :(
 
S

Stefan Blom

You have to be more specific. What is the macro doing? Show us your code.

As an alternative, note that you can use AutoCorrect to replace one string
of text with some (other) data. I'm not sure that you can use it to add
spaces, though, because AutoCorrect replacements are triggered by spaces (or
punctuation plus a space).

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Anne J" wrote in message
For example: I have a bear.He is nice.

How do I add a space between "." and "He"? Assuming that it is only
applicable to ^$ (letters). Which means that if there is a number after a
dot ".", the rule won't apply.

This is for Word 2003.

Please help me if someone knows. Thank you so much.
 
E

Ed Weber

It is likely that an unwanted line break was inserted by email. If the red
text is between two lines which start with an apostrophe, type an apostrophe
in front of the red text.
I am sending the macro as text file to your email address. Open the file
in NotePad, then copy the macro and paste it into the VBE. Go to
http://www.productivitytalk.com/forums/index.php?showtopic=3788 and download
a document which explains how to install a macro.

--


Anne J said:
I have Word 2003. I copied all the text you posted in the VBA field, but it
all turned red and doesn't work.. :(





---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 120229-0, 02/29/2012
Tested on: 2/29/2012 2:28:27 PM
avast! - copyright (c) 1988-2012 AVAST Software.
http://www.avast.com
 
A

Anne J

I just want to know what Ed Weber wrote. He wrote some macro which could do the job well, but I don't know how to copy it cause it's all messed up..:(

I need a script which would allow me to add space after/and before certain words and/or letters/numbers. Let's say I want to add space before numbers 1-8, but not before number 9.

So, I have a popsicle. 6 of them.

and

So, I have a popsicle.9 of them. Space has been put before 6, but Not before 9. Let's say I have to be more specific regarding certain numbers, letters, words after which/or before I have to put space. I need a script in VBA which would allow me to add/remove those exceptions at will.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
Ed's macro is OK, except for a line-wrapping issue. Try:
Code:
Sub SpaceAfterPeriod()
'Inserts a space after the period when the text is $.$ where $ represents any letter.
'Does not insert the space, if a space already exists as in $. $
'Does not insert a space if a number precedes or follows the period.
ActiveDocument.Range.Find.Execute Findtext:="([A-Za-z].)([A-Za-z])", _
MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _
Replace:=wdReplaceAll
End Sub
An easier-to-read version is:
Code:
Sub SpaceAfterPeriod()
'Inserts a space after the period when the text is $.$ where $ represents any letter.
'Does not insert the space, if a space already exists as in $. $
'Does not insert a space if a number precedes or follows the period.
With ActiveDocument.Range.Find
  .Text = "([A-Za-z].)([A-Za-z])"
  .MatchWildcards = True
  .Wrap = wdFindContinue
  .Replacement.Text = "\1 \2"
  .Execute Replace:=wdReplaceAll
End With
End Sub
As for being able to choose the numbers, you'd need to add the ones to be processed to the macro's find expression. For example:
.Text = "([A-Za-z1-8].)([A-Za-z])"
to add the numbers 1-8 to the characters that, if followed by a period then a letter, should have a space added after the period.
 
S

Stefan Blom

It seems as if the commented lines got wrapped to the next line. Try copying
this version instead:

Sub SpaceAfterPeriod()
'Inserts a space after the period when the text is $.$
'where $ represents
'any letter.
'Does not insert the space, if a space already exists as in $. $
'Does not insert a space if a number precedes or follows the period.
ActiveDocument.Range.Find.Execute Findtext:="([A-Za-z].)([A-Za-z])", _
MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _

Replace:=wdReplaceAll
End Sub

Just in case, test the macro on a copy of your document first.

-- 
Stefan Blom
Microsoft Word MVP




---------------------------------------------
"Anne J" wrote in message
I just want to know what Ed Weber wrote. He wrote some macro which could do
the job well, but I don't know how to copy it cause it's all messed up..:(

I need a script which would allow me to add space after/and before certain
words and/or letters/numbers. Let's say I want to add space before numbers
1-8, but not before number 9.

So, I have a popsicle. 6 of them.

and

So, I have a popsicle.9 of them. Space has been put before 6, but Not before
9. Let's say I have to be more specific regarding certain numbers, letters,
words after which/or before I have to put space. I need a script in VBA
which would allow me to add/remove those exceptions at will.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top