PDA

View Full Version : Removing multiple spaces with track changes enabled



tlangford
05-05-2008, 07:53 AM
Hi.

I used some code remove multiple spaces, which works really well. However, when I run the same code with track changes enabled, the results are completely wrong.

I can understand why this is happening, as the changes it makes in each iteration of the Selection.Find call are "pending", as they haven't been accepted yet.

:help Does anyone know how to have the code sucessfully remove multiple spaces, while tracking the changes? Btw, I'm tracking changes as the people I'm writing this for want to see what changes my macro is making to their documents.

Code follows...


Public Sub ReplaceMultipleSpaces()
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
'Here is where it is actually looking for spaces between words
.Text = " [ ]@([! ])"
'This line tells it to replace the excessive spaces with one space
.Replacement.Text = " \1"
.MatchWildcards = True
.Wrap = wdFindStop
.Format = False
.Forward = True
'execute the replace
.Execute Replace:=wdReplaceAll
End With
End Sub

fumei
05-05-2008, 08:41 AM
" the results are completely wrong."

What does that mean?

I tested it and it does indeed remove double spaces, and does indeed mark the changes as tracked changes.

So, what is "wrong"?

tlangford
05-05-2008, 12:19 PM
Thanks for the fast reply. I've tested the code on both Word 2002 and 2003 with the same result.

If I enter "This is a test" (double space after a) and run the code without track changes enabled, it works fine, but if I turn tracking on, I get the following:

"This is at est". There are two tracked changes. The first is "Deleted: (space)" and the other is "Deleted: (space)t".

It seems like it removes the first space just fine, but then gets confused. It seems like it's something to do with the "pending" tracked deletion, and it ends up deleting the valid characters, a space and the t.

Are you really unable to replicate this? Can anyone else replicate it?

fumei
05-05-2008, 12:50 PM
Yes, I can replicate this. Try:

This is a test - 3, or 4, extra spaces.

with a bunch of spaces.

Next, try This is a test (with ONE extra space) with:

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
' Here is where it is actually looking for
' spaces between words
.Text = " "
' This line tells it to replace the excessive
' spaces with one space
.Replacement.Text = " "

and look up what your .Text = " [ ]@([! ])" actually does.

tlangford
05-15-2008, 11:52 PM
Fumei, were you just confirming that this is in fact a problem? Have you got a solution? : pray2: