PDA

View Full Version : [SOLVED:] Space after punctuation -macro



Programmer_n
04-17-2016, 03:25 AM
I need a space after punctuation. The code below helps, when the track changes is off. But when Track changes is on it gives funny results. To avoid leaving a space in decimal digits of number, the criteria is to find a letter before and after the punctuation. But, I am facing a strange problem if the track changes is on.

Sample sentence.

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document.You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.

Works fine with with track changes switched off.

ActiveDocument.Range.Find.Execute Findtext:="([A-Za-z].)([A-Za-z])", _
MatchWildcards:=True, Wrap:=wdFindContinue, Replacewith:="\1 \2", _
Replace:=wdReplaceAll

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document. You can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.

For same code final outcome with Track changes on

On the Insert tab, the galleries include items that are designed to coordinate with the overall look of your document.t Y.Y ou can use these galleries to insert tables, headers, footers, lists, cover pages, and other document building blocks.

Is there a better approach to solve my problem. One more thing I used an auto-correct code to get the first auto suggestion for spelling and spacing problem. It works for most cases except for words containing punctuation in-between like (document.you). I am not sure, Why?

gmaxey
04-17-2016, 05:23 AM
Won't work as fast:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "[A-Za-z].[A-Za-z]"
.MatchWildcards = True
.Wrap = wdFindContinue
While .Execute
With oRng
.MoveStart wdCharacter, 2
.InsertBefore " "
.Collapse wdCollapseEnd
End With
Wend
End With
lbl_Exit:
Exit Sub
End Sub

Programmer_n
04-17-2016, 05:36 AM
gmaxey the code works good.

In case if the fullstop punctuation happens to follow ) or " how to amend the .Text?

Because,

.Text = "[A-Za-z]).[A-Za-z]" does gives pattern matching errror. Which I can understand because of unopened parentheses.

Please help to solve on how to make .Text understand pattern like ". or pattern like ). etc.

gmaxey
04-17-2016, 09:40 AM
.Text = "[A-Za-z\)].[A-Za-z]"