Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MS Word Macro Remove Blank Lines Before a page break

Status
Not open for further replies.

dodge20

MIS
Jan 15, 2003
1,048
US
I currently have a macro that finds text and places a page break in front of it, which works fine. The problem is I have a variable number of blank lines before each page break. I would like that variable number to be 3. Currently I am doing a find replace many times and replacing
^p^p^p^p
with
^p^p^p

I am wondering if there is a way to loop through the word document to perform this instead of me doing a find and replace 20+ times?

My macro to insert the page break is

Code:
Dim strData As String
strData = InputBox("Paste Text where you want the page break inserted")
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = (strData)
        .Replacement.Text = "^m" & (strData)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    ActiveWindow.ActivePane.VerticalPercentScrolled = 0
End Sub

Dodge20
 




Please post VBA questions in Forum707.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 





FYI,

Replace TWO adjacent Paragraphs with ONE.

Loop until NO REPLACEMENT occurs.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks I will repost.

The replacing 2 adjacent paragraphs with 1 won't work because there are other places where there are 2 blank line and i want to keep those. I just want to remove the blank lines before the page breaks.

Dodge20
 




"...there are other places where there are 2 blank line and i want to keep those"

That is not the best & accepted way of creating space in a document.

Check out STYLES.

You should NEVER have two adjacent paragraph characters if you are employing Word's features as designed!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I don't understand the requirement for a number of empty paragraphs before a hard page break. If there is room for them on the page before the break they will have no effect - and if there isn't room, they will create a blank page - is that what you really want?

If you really want to do it, I would suggest a wildcard replace - find [blue]^13@^m[/blue] and replace all with [blue]^p^p^p^m[/blue]

If what you really want is simply to remove (all) the excess blank lines then just replace all [blue]^13@^m[/blue] with [blue]^m[/blue] (again with wildcards checked)

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Hi Tony

When I search for
^13@^m (with wildcards checked)

I get 0 results. There must be something I am doing wrong

Dodge20
 
It shouldn't make any difference but you could try [blue]^13{1,}^m[/blue]. Other than that you must, as you say, be doing something wrong but it's hard to guess what. Do you have any formatting included on the Find? Or a selection made at the time?

And - what version of Word are you using?

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top