macro to take out spaces

  • Thread starter Thread starter boris
  • Start date Start date
B

boris

I need a macro that will take out the multiple spaces in a
document and convert them to single spaces. There is a
conversion problem with getting a document from one format
to rtf, and the person using the resulting rtf is spending
hours getting rid of the multiple spaces, turning them
into normal, single spaces.

If someone can provide a macro that will scrub the doc for
multiple spaces and convert to singles, would help
greatly. I don't know Word macros, hence the request.

Thx

Boris
 
Find and replace will do this in a number of ways, including --

search for ^w and replace with a single space. That replaces any sequence of
white space characters with a single space. Includes tabs, which you might
not want to replace.

with wildcards enabled, search for [space]{2,} (single space then {2,}),
replace with a single space. That replaces any sequence of two or more
spaces with a single space.

search for two spaces and replace with one space. Click Replace All
repeatedly until the number of replacements stops changing.
 
Or adapted to the macro solution - see
http://www.gmayor.com/installing_macro.htm

Sub SingleSpace()
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "([ ]){2,}"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute replace:=wdReplaceAll
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Find and replace will do this in a number of ways, including --

search for ^w and replace with a single space. That replaces any
sequence of white space characters with a single space. Includes
tabs, which you might not want to replace.

with wildcards enabled, search for [space]{2,} (single space then
{2,}), replace with a single space. That replaces any sequence of two
or more spaces with a single space.

search for two spaces and replace with one space. Click Replace All
repeatedly until the number of replacements stops changing.



boris said:
I need a macro that will take out the multiple spaces in a
document and convert them to single spaces. There is a
conversion problem with getting a document from one format
to rtf, and the person using the resulting rtf is spending
hours getting rid of the multiple spaces, turning them
into normal, single spaces.

If someone can provide a macro that will scrub the doc for
multiple spaces and convert to singles, would help
greatly. I don't know Word macros, hence the request.

Thx

Boris
 
Jezebel, thanks very much. I did multiple searches for
double spaces and replaced with single. I still have the
following problem. I have:

1) lines ending in carriage return, with the following
lines being just carriage returns
2) line ending in carriage return, with the following
lines being space followed by carriage returns
3) after taking out all double spaces that were leading
each line, I now still have one space at the beginning of
those lines, but cannot search for simply single spaces,
since this would take every space. Is there a way to find
carriage return followed by space, and replace it with
just carriage return?

I think what I need is to know how to represent a carriage
return in the find/replace dialogue. Then I can come up
with the combinations I need to find them. If you happen
to know what tab is, that would help also.

Thanks much.

boris
-----Original Message-----
Find and replace will do this in a number of ways, including --

search for ^w and replace with a single space. That replaces any sequence of
white space characters with a single space. Includes tabs, which you might
not want to replace.

with wildcards enabled, search for [space]{2,} (single space then {2,}),
replace with a single space. That replaces any sequence of two or more
spaces with a single space.

search for two spaces and replace with one space. Click Replace All
repeatedly until the number of replacements stops changing.



I need a macro that will take out the multiple spaces in a
document and convert them to single spaces. There is a
conversion problem with getting a document from one format
to rtf, and the person using the resulting rtf is spending
hours getting rid of the multiple spaces, turning them
into normal, single spaces.

If someone can provide a macro that will scrub the doc for
multiple spaces and convert to singles, would help
greatly. I don't know Word macros, hence the request.

Thx

Boris


.
 
Of course. Search for <space>^p and replace with ^p. If I were doing this, I
would then search for ^p^p and replace with ^p.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

boris said:
Jezebel, thanks very much. I did multiple searches for
double spaces and replaced with single. I still have the
following problem. I have:

1) lines ending in carriage return, with the following
lines being just carriage returns
2) line ending in carriage return, with the following
lines being space followed by carriage returns
3) after taking out all double spaces that were leading
each line, I now still have one space at the beginning of
those lines, but cannot search for simply single spaces,
since this would take every space. Is there a way to find
carriage return followed by space, and replace it with
just carriage return?

I think what I need is to know how to represent a carriage
return in the find/replace dialogue. Then I can come up
with the combinations I need to find them. If you happen
to know what tab is, that would help also.

Thanks much.

boris
-----Original Message-----
Find and replace will do this in a number of ways, including --

search for ^w and replace with a single space. That replaces any sequence of
white space characters with a single space. Includes tabs, which you might
not want to replace.

with wildcards enabled, search for [space]{2,} (single space then {2,}),
replace with a single space. That replaces any sequence of two or more
spaces with a single space.

search for two spaces and replace with one space. Click Replace All
repeatedly until the number of replacements stops changing.



I need a macro that will take out the multiple spaces in a
document and convert them to single spaces. There is a
conversion problem with getting a document from one format
to rtf, and the person using the resulting rtf is spending
hours getting rid of the multiple spaces, turning them
into normal, single spaces.

If someone can provide a macro that will scrub the doc for
multiple spaces and convert to singles, would help
greatly. I don't know Word macros, hence the request.

Thx

Boris


.
 
no sooner do I say it's exactly what I needed, than I come
up with another problem...first of all, is there some
place that lists all the combinations like ^p which
represent non-alphanumeric characters? second, for some
reason, when I first searched for ^p^p and replaced with
^p, it found several matches. However there are still
some that appear to be there (I have the paragraph marks
on), and yet it won't find those when I run the search
again. Any idea why this might be? Thx for the continued
help.

Boris
 
If you expand the Find and Replace dialog (click More) and click on Special,
you will find a list of such characters. The list is different for "Find
what" and "Replace with," and both change if you enable wildcards. For a
complete list, see "WD97: Control Codes to Use with Find and Replace (Edit
Menu)" at http://support.microsoft.com/?kbid=197855

It has been discovered that for some reason a Replace operation won't remove
an empty paragraph before a table. This is inconvenient, but there is
apparently no workaround (not even through VBA IIRC).

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Suzanne, thanks again, so other than tables, there
shouldn't be any reason that the double paragraph mark
would not be found? Odd, as this document is just one
long series of lines, which now read text, carriage,
carriage, text, etc. Cannot figure it out for the life
of me.

If you can think of anything else, let me know. Would it
be possible for me to email the doc offline (at least a
portion of it) to see if you notice anything? I keep my
machine updated with anti-virus every day, so there
should not be a threat.

Boris
 
Occasionally there appear strange characters (especially in text pasted from
the Web) that are displayed as paragraph breaks but are actually line
breaks. If ^l (that's a lowercase L) doesn't find them, then you can try
^13, which will sometimes find one of these anomalies.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Suzanne, thanks so much. Don't know what the ^13 was,
but it found what I had thought were simple paragraph
markers. Unless my contact gets back to me saying that
the document still has formatting issues, I think the
combination of ^p and ^13 will work going forward.
Thanks so much.

Boris
 
Glad it worked for you.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA

Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
Back
Top