Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS Word Delete Empty Space

Posted on 2014-09-03
8
Medium Priority
?
620 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
Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

 
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 2000 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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

715 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