Remove Pagebreak before Heading 1

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Remove Pagebreak before Heading 1

Post by Robie »

Hi

I have few documents (will get some more) with 'Page Break followed by Heading 1 paragraph'. Now, how do I get rid of 'page breaks' without affecting the Heading 1 paragraph? Everytime I do anything, it *removes* the numbering from the Heading 1 paragraph. Page break also has Heading 1 style.
pagebreak-h1.png
I have tried several different wildcard tyle find/replace but nothing seems to work. Any idea how I do this?

Thanks
You do not have the required permissions to view the files attached to this post.

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Remove Pagebreak before Heading 1

Post by Robie »

OK. Had further inspiration and tried the following. I recorded a macro that worked fine when recorded it but the code below is not! Don't understand.

Code: Select all

Sub RemovePageBreaksBeforeHeading1()
'
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Heading 1")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Normal")
    Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
        .Text = "^m"
        .Replacement.Text = "xxxyyy^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .matchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    
    Selection.Find.ClearFormatting
    Selection.Find.Style = ActiveDocument.Styles("Normal")
    Selection.Find.ParagraphFormat.Borders.Shadow = False
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Style = ActiveDocument.Styles("Normal")
    Selection.Find.Replacement.ParagraphFormat.Borders.Shadow = False
    With Selection.Find
        .Text = "xxxyyy"
        .Replacement.Text = "^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .matchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

User avatar
HansV
Administrator
Posts: 78621
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Remove Pagebreak before Heading 1

Post by HansV »

You should probably omit the references to borders from the macro. Does this do what you want?

Code: Select all

Sub RemovePageBreaksBeforeHeading1()
  With Selection.Find
    .ClearFormatting
    .Style = ActiveDocument.Styles(wdStyleHeading1)
    .Replacement.ClearFormatting
    .Replacement.Style = ActiveDocument.Styles(wdStyleNormal)
    .Text = "^m"
    .Replacement.Text = "^p"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute Replace:=wdReplaceAll
  End With
End Sub
I replaced the style names with symbolic constants, so that the macro will run in other language versions of Word too.
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: Remove Pagebreak before Heading 1

Post by Robie »

Thanks Hans. Removing the border references fixed it.

BTW: Is there a way to remove the empty paragraph it leaves behind?

To put it another way, it possible to remove page breaks without affacting the next paragraph and *not* leaving the empty paragraph behind?

User avatar
stuck
Panoramic Lounger
Posts: 8191
Joined: 25 Jan 2010, 09:09
Location: retirement

Re: Remove Pagebreak before Heading 1

Post by stuck »

Have you tried:

1) Putting the cursor in the Heading 1 paragraph
2) Setting that paragraph to 'page break before' (Format|paragraph|line & page breaks tab)
3) Selecting the page break and deleting it and any other unrequired paragraphs above the Heading 1

You will also need to make sure the style Heading 1 is not set to automatically update to reflect changes otherwise all your Heading 1 paragraphs will page break before.

Ken