Solved

MS Word Delete Empty Space

Posted on 2014-09-03
8
543 Views
Last Modified: 2014-09-04
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.
0
Comment
Question by:mdstalla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40302521
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
0
 

Author Comment

by:mdstalla
ID: 40302707
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?
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40302819
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

0
[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40302853
I missed the possibility of more than two empty paragraphs in your previous question: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_28501195.htm
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

0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40302920
@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.
0
 

Author Comment

by:mdstalla
ID: 40303875
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)?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 40304168
mdstalla,

I was hoping that you would be able to work that out. As I indicated in your other question, maintainability might be a problem because you are using VBA to bend Mail Merge away from its original purpose which was originally intended to be a code-less operation. A working knowledge of VBA will be required to support your project.

However here is the new code shown replacing the old code, which is still visible but has been commented out.
Sub Document_Open()
    ThisDocument.Fields.Locked = False
    With ThisDocument.MailMerge
        .MainDocumentType = wdDirectory
        .Destination = wdSendToNewDocument
        .Execute
    End With
    EditResultDocument
End Sub

Public Sub EditResultDocument()

    With ActiveDocument
        With .Range.Find
            Do
               'Delete flagged paragraphs. Loop to overcome problem of missed adjacent paragraphs
                .Text = "^13Delete--*^13"
                .Replacement.Text = "^p"
                .MatchWildcards = True
            Loop While .Execute(Replace:=wdReplaceAll)
        End With
        With .Range.Find
            'delete paragraphs that only contain spaces
            .Text = "^13[ ]{1,}^13"
            .Replacement.Text = "^p"
            .Execute Replace:=wdReplaceAll
           
            'remove manual page breaks
            .MatchWildcards = False
            .Text = "^m"
            .Replacement.Text = ""
            .Execute Replace:=wdReplaceAll
           
           ' 'remove empty paragraphs (old)
           ' .MatchWildcards = False
           ' .Text = "^p^p"
            '.Replacement.Text = "^p"
            '.Execute Replace:=wdReplaceAll

               '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
        'remove inter-paragraph vertical space
        .Range.Paragraphs.SpaceAfter = 0
        .Range.Paragraphs.SpaceBefore = 0
       
    End With
    ActiveDocument.SaveAs Replace(ThisDocument.FullName, ".docm", ".docx", , , vbTextCompare), wdFormatXMLDocument
    ThisDocument.Close wdDoNotSaveChanges
End Sub
0
 

Author Closing Comment

by:mdstalla
ID: 40304927
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question