remove manual page breaks before headings and leave the others untouched

A

andreas

Hello,

is it possible to tell Word VBA to remove only all manual page breaks
immediately! preceding headings that are formatted with the built-in
heading paragraph style "heading 1" and leave the other page breaks
untouched.

Example

text text text text text text text text text text
------------------------------------------- page break (manual)
Heading 1: The analysis etc.

Should there be a blank paragraph (paragraph mark) between the page
break and heading 1, the page break should not be removed. It should
only be removed if the heading 1 follows directly after.

Kind of a weird request, nevertheless I wonder whether this could be
achieved.

Help is appreciated. Thank you very much in advance.

Regards,

Andreas
 
H

Helmut Weber

Hi Andreas,
Kind of a weird request

not at all.

Like this:

Sub Macro9AA()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Style = "Heading 1"
While .Execute
If rngDcm.Characters.First = Chr(12) Then
rngDcm.Characters.First = ""
End If
Wend
End With
End Sub

There are other ways, of course.

To set a character to "" prevents
autocorrection from executing,
which doesn't matter in this context,
does no harm either,
but prevents endless loops and other complications
in other contexts.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
A

andreas

Hi Andreas,


not at all.

Like this:

Sub Macro9AA()
Dim rngDcm As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Style = "Heading 1"
While .Execute
If rngDcm.Characters.First = Chr(12) Then
rngDcm.Characters.First = ""
End If
Wend
End With
End Sub

There are other ways, of course.

To set a character to "" prevents
autocorrection from executing,
which doesn't matter in this context,
does no harm either,
but prevents endless loops and other complications
in other contexts.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Helmut,

thank you very much. As always the macro is working. Good job.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top