Inserting a section break before each heading level 1 automatically

A

andreas

Dear Experts:

I tried to write a macro that is supposed to look for custom numbered
heading styles, level 1 ("custom_heading_1") in a document and insert a
section break (next page) automatically before these headings.

Running the below macro I get an error saying that "parameter value was
out of acceptable range" and the macro line "Selection.InsertBreak
Type:=wdSectionBreakNext" gets highlighted.

I guess I have to define a range or something.

Help is appreciated. Thank you very much in advance.



Sub InsertSection()

Selection.HomeKey unit:=wdStory

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("custom_heading_1")

With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindStop
.Execute
End With

While Selection.Find.Found

Selection.InsertBreak Type:=wdSectionBreakNext

Selection.Find.Execute

Wend

End Sub
 
H

Helmut Weber

Hi Andreas,

sometimes things turn out to be unexpectedly complicated,
and most often, too, having reached the primary goal,
there are unwanted sideeffects, which have to be controlled as well.

IMHO, the following inserts a page break before
a paragraph of the given style *and*
can be repeated without inserting an additional
page break again, which was the unwanted sideeffect.

Be aware, it is wdSectionBreakNextPage,
not wdSectionBreakNext.

Sub Test4454()
Dim rngDcm As Range
Dim rngTmp As Range
Set rngDcm = ActiveDocument.Range
With rngDcm.Find
.Style = "Heading 3"
While .Execute
rngDcm.Select ' for testing only
Set rngTmp = rngDcm.Duplicate
rngTmp.Collapse
rngTmp.Select ' for testing too
If Asc(rngTmp.Characters.First.Previous) <> 12 And _
Asc(rngTmp.Characters.First) <> 12 Then
rngTmp.InsertBreak Type:=wdSectionBreakNextPage
End If
rngDcm.Collapse Direction:=wdCollapseEnd
Wend
End With
End Sub

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

andreas

Hi Helmut,

very good job. It is working fine. I ran the macro once and it inserted
the sections at the right places. I added a couple of more headings
level 1 to the document and was afraid, that on
re-running the macro it would add another section break to the ones
that are already there.
It did not. Great!! How come? Which macro line is causing this ?

Thanks again

Regards,

Andreas
 
H

Helmut Weber

Hi Andreas,
very good job. It is working fine. I ran the macro once and it inserted
the sections at the right places. I added a couple of more headings
level 1 to the document and was afraid, that on
re-running the macro it would add another section break to the ones
that are already there.
It did not. Great!! How come? Which macro line is causing this ?

If Asc(rngTmp.Characters.First.Previous) <> 12 And _
Asc(rngTmp.Characters.First) <> 12 Then
rngTmp.InsertBreak Type:=wdSectionBreakNextPage
End If

If the first character in the range in not a sectionbreak
and the character before the first character isn't
a sectionbreak either, then...

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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

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