Link to home
Start Free TrialLog in
Avatar of mdstalla
mdstalla

asked on

MS Word Delete Empty Space

Experts:

I would like to create a MS Word Macro that, when run, shifts all paragraphs up so that there are no large gaps of empty space between each of my paragraphs.  For example, my current document looks like this:

Please Describe Yourself


My name is John Doe. I live at 123 Main Street, Denver, CO 80214





I am a boy.  I am 12 years old and I go to Park Meadows Middle school.
I like football, soccer, baseball and hockey.


I would like the macro to restructure the document so it looks like this:


Please Describe Yourself
My name is John Doe. I live at 123 Main Street, Denver, CO 80214
I am a boy.  I am 12 years old and I go to Park Meadows Middle school.
I like football, soccer, baseball and hockey.


Does anyone have a code to make this happen?

Thanks.
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Simply record a replacement of ^p^p with ^p (^p is the Word code for paragraph). This will replace any 2 consecutive paragraph signs with a single one.

Then save that macro, give it a hot key and run it whenever you want.

HTH,
Dan
Avatar of mdstalla
mdstalla

ASKER

The problem is a little more complicated than that.  Let's just assume that I need to apply this macro to dozens of reports that can be structured in any combination of ways (the sample showed you was just a simple example).  

There can be one ^p dividing a paragraph, two, ten... etc...  And this will change for every report I work with.  

In other words, I need a Macro code that simply says "If ^p^p (or greater) exists, replace with ^p.
Any idea what this code looks like?
Yeah. Something like this:
Selection.Find.Text = "^p^p"
Selection.Find.Replacement.Text = "^p"
Selection.Find.Execute
While Selection.Find.Found
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute
Wend

Open in new window

I missed the possibility of more than two empty paragraphs in your previous question: https://www.experts-exchange.com/questions/28501195/Macro-for-MS-Word-Mail-Merge-Formatting.html
It can be done in a single step using wildcarding:
With ActiveDocument.Range.Find
    'remove empty paragraphs
     .MatchWildcards = True
     'at least two consecutive paragraph marks
     .Text = "[^13]{2,}" '(^p) doesn't work with wildcard find .Text, so use ^13
     .Replacement.Text = "^p" 'replace with one
     .Execute Replace:=wdReplaceAll
End With

Open in new window

@Graham: your solution is cleaner. The only redeemer for my solution is that it works from the cursor onward, so you can keep the spacing on the titles, if you want.
I'll try both...

@Graham-- Where do I put your code?  Is this a macro or do I add it to the other codes you gave me (this document, module)?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know Graham-- your codes seem to be working perfectly!  

Each time I open the Word document, it deletes the appropriate paragraphs and shifts all remaining test upward in just the right spots.  I've run a number of tests changing the order of 'Delete--'s on my Excel spreadsheet, and when I go back and open the Word Document, it continues to adjust appropriately.    In addition, there are no additional (unnecessary) Word documents being displayed.

For your information, there will never be more than one record-set from Access being exported to Excel at any given time... so we don't have to worry about how this procedure handles multiple record sets.

Of course the example report I've shared with you (and run to test your codes) is an extremely simplified prototype... my actual project is a document with hundreds of paragraphs that need to remain or be deleted based on conditions met in my Access database... so we'll see how these codes work on a large document.

But as far as I'm concerned at this point, you've adequately solved my problem and I now consider you a Programming God!
Thank you so much for all of your hard work.