Solved

MS Word Delete Empty Space

Posted on 2014-09-03
8
498 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

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 34

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 34

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.

803 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