Solved

MS Word Delete Empty Space

Posted on 2014-09-03
8
478 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
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 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 Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now