Delte all manual page breaks before built-in heading style 1 and leave all other ones untouched

A

andreas

Dear Experts,

I wonder whether it is possible to delete all manual page breaks (Strg
+Enter), if any, immediately preceding the headings formatted with the
built-in heading style level 1 in a document and leave all other
manual page breaks (Strg+Enter) untouched.

Help is much appreciated. Thank you very much in advance.

Regards,
Andreas
 
J

Jean-Guy Marcil

andreas was telling us:
andreas nous racontait que :
Dear Experts,

I wonder whether it is possible to delete all manual page breaks (Strg
+Enter), if any, immediately preceding the headings formatted with the
built-in heading style level 1 in a document and leave all other
manual page breaks (Strg+Enter) untouched.

Help is much appreciated. Thank you very much in advance.

Try something like this:

'_______________________________________
Option Explicit

'_______________________________________
Sub DelPageBreak()

Dim rgeDoc As Range

Set rgeDoc = ActiveDocument.Range

With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Next.Paragraphs(1).Range.Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With

Application.Browser.Target = wdBrowsePage

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bear

Jean-Guy:

Can you tell me what the parent of the page break character is? I mean what
kind of object or Word component?

Bear
 
J

Jean-Guy Marcil

Bear was telling us:
Bear nous racontait que :
Jean-Guy:

Can you tell me what the parent of the page break character is? I
mean what kind of object or Word component?

It is a Range object that is the "parent" of the actual found text (Text is
a property of the Range object).
It is not so much that I am using the Parent of the Page Break. It just
happens that the Find method is searching for a Page break, and the Parent
object represent the Range of that found text, if it is found, of course.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Bear,

when using the find-method of a range
and found is true (while execute),
then the range shrinks to the found spot.

The parent of "rgeDoc.Find" is "rgedoc".
Then Jean-Guy checks rgeDoc's next paragraph.
If the condition is true, then "rgedoc" will be deleted.

After that, the range expands again, temporarily,
seemably programmatically not accessable,
from the end it had before to the end of the doc.
If there was a found spot again,
the range shrinks to it.
If not, it shrinks again to where it was before.

Sub TestA()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "xx"
.Parent.Select
While .Execute
.Parent.Select
Wend
End With
End Sub

Cheers everybody

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Bear

Jean-Guy:

I think I understand. So

rgedoc.find.parent is a range
rgedoc.find.parent.next is the next character
rgedoc.find.parent.next.paragraphs(1) is the paragraph that character is in

I guess rgedoc.find.Parent.next(wdparagraph).style would work also.

Bear
 
B

Bear

Helmut, Jean-Guy, and Andreas:

I guess I'll need to experiment some before I get the nuances of this.

I think this will work exactly the same:

With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Paragraphs(1).Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With

I can understand how within the With rgeDoc.Find I have to use .Parent to
move up from the Find object to the Range object, but I still don't get the
Next part.

Bear
 
J

Jean-Guy Marcil

Bear was telling us:
Bear nous racontait que :
Helmut, Jean-Guy, and Andreas:

I guess I'll need to experiment some before I get the nuances of this.

I think this will work exactly the same:

With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Paragraphs(1).Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With

It does, but only because we are dealing with Manual Page breaks, which
always have the same style as the following paragraph. If you were dealing
with section breaks, which can have a style of their own, like a ¶ does,
then your code would not work all the time, but mine would, because of the
next thing....
I can understand how within the With rgeDoc.Find I have to use
.Parent to move up from the Find object to the Range object, but I
still don't get the Next part.

I guess I overdid it because in this particular case, the Next was not
necessary...

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

andreas

andreas was telling us:
andreas nous racontait que :




Try something like this:

'_______________________________________
Option Explicit

'_______________________________________
Sub DelPageBreak()

Dim rgeDoc As Range

Set rgeDoc = ActiveDocument.Range

With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Next.Paragraphs(1).Range.Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With

Application.Browser.Target = wdBrowsePage

End Sub
'_______________________________________

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:http://www.word.mvps.org


Jean-Guy,
great! Thank you very much. It is working as desired.
Regards, Andreas
 
A

andreas

Hi Bear,

when using the find-method of a range
and found is true (while execute),
then the range shrinks to the found spot.

The parent of "rgeDoc.Find" is "rgedoc".
Then Jean-Guy checks rgeDoc's next paragraph.
If the condition is true, then "rgedoc" will be deleted.

After that, the range expands again, temporarily,
seemably programmatically not accessable,
from the end it had before to the end of the doc.
If there was a found spot again,
the range shrinks to it.
If not, it shrinks again to where it was before.

Sub TestA()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Text = "xx"
.Parent.Select
While .Execute
.Parent.Select
Wend
End With
End Sub

Cheers everybody

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

Helmut,

thanks a lot for your in-depth explanation of Jean-Guy's code.
Regards, Andreas
 

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