Macro to delete spaces in doc

H

Heather

I am wanting to create a macro that will delete spaces (hard returns) between
paragraphs. Can this be done?
 
G

Graham Mayor

You could search and replace ^p with a space, but that would give you one
large paragraph. If you want to retain the paragraphs apply a style with no
space before or after - such as the default normal style.
If you have extra hard returns see
http://word.mvps.org/FAQs/Formatting/CleanWebText.htm

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
F

Fledermaus

It would be helpful to know what you're trying to achieve. Do you want one
long para? Or is it that you have extra hard returns you want to get rid of?
Or? What is the end result you want to achieve?
 
J

jerem

If you want to just get rid of the excess hard returns between paragraphs,
this is what I use:

Sub DelEmptyParas()
'
' DelEmptyParas Macro
' Macro created 4/13/2007 by debrawallace
'
Dim i As Integer
i = 10
Do
i = i - 1
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Loop Until i = 0
End Sub

If you want to get rid of paragraph marks at the end of every line and
replace them with spaces (sometimes when you scan documents, they come over
with a paragraph mark at the end of every line instead of having a space) -
this is what I do: first I highlight all of the paragraph marks short of the
last one then I run this macro:

Sub PartoSpa()
'
' PartoSpa Macro
' Macro recorded 3/29/2007 by debrawallace
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Hope this helps.
 
H

Heather

Thank for the reply, Jerem.

I think you're on the right track to what I'm trying to do. I pull data over
into a Word template from a database, and when it does, it puts double spaces
between each paragraph. That's what I'm trying to get rid of. I ran the
first code that you had and it didn't work for that purpose. Any suggestions
on that?

Thanks!
 
H

Heather

Thanks for the reply, Fledermaus.

When I pull data over into a Word template from a database, it puts double
spaces between each paragraph. That's what I'm trying to get rid of so that
it only leaves one space between each. Does that makes sense?

Thanks!
 
G

Graham Mayor

Replace
^13{1,}
with
^p
with the 'use wildcards' option checked. This will replace all multiple
paragraph breaks with a single one. If there remains excessive space after
that, it will be attributable to added spacing in the paragraph style
definition.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

or by macro

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13{1,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With

If that doesn't work send a sample of the document to the link on my web
site so I can see what the problem might be.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jerem

If you read the first block of code I sent you -- I set up a counter to 10
and said every time you see two paragraph marks in a row, replace it with one
paragraph mark. The first run cuts the count of two paragraphs in half, the
second run cuts that number in half and so on and so forth (sort of like a
half-life in chemistry) until you no longer have two paragraph marks in a
row. It works wonderfully for me, however, if you have any character before
the excess paragraph mark you want to delete it won't work because it is only
looking for two paragraph marks back to back.

Is there anything between the first paragraph mark and the second paragraph
mark? Other than that, I can't see why it wouldn't work for you. That macro
saves me a lot of time. The only other thing I can think of is why it
wouldn't work is if they are soft returns rather than hard returns, but I
take it from your description that they are hard returns. Anything
particular about the Word template your moving the data into?
 
J

jerem

One more thing that I forgot to say in my previous response.

Open a blank document and type in some paragraphs with hard returns in
between, then run the DelEmpPar code I sent you. If the macro works, there's
something different about your Word template document and you're going to
have to pinpoint what that difference is to address the problem.

Let me know how that works out.
 
A

Access101

I first replace all ^p^p with @@
Then I replace the ^p with a space
Then I replace the @@ with ^p^p

This gives me my double spacing between paras, but gets rid of the paramarks
 
G

Graham Mayor

This is poor formatting - much better to use paragraph styles with added
space, then you don't have this rigmarole when you want to change the
layout.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
R

Russ

This will replace two or more space characters after each paragraph with one
space character.

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^13 {2,}" 'MacWord use \n for ^13
.Replacement.Text = "^p "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
 

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