Insert PageBreak before certain Styles

A

Access101

I would like to insert a page break before any paragraph that has a style
starting with "Heading". This code Inserts XXPBXX and is later
search/replaced by a page break. This code runs very slow, and there must be
an easier way.

Dim doc As Document
Set doc = ActiveDocument
pCount = doc.Paragraphs.Count
Application.ScreenUpdating = False
For p = 1 To pCount
If Left(doc.Paragraphs(p).Style, 7) = "Heading" Then
doc.Paragraphs(p).Range.Select
Selection.InsertBefore "XXPBXX"
End If
Next
Application.ScreenUpdating = True
 
J

Jay Freedman

Don't even think of doing it this way! Besides the slow and clunky code,
you'll get each page break in the Heading x style, which can cause problems
if you use a Table of Contents or apply a shading or border to the heading.
And if you add a heading, you have to add a page break for it; and if you
delete a heading, you have to remember to delete its page break. There's a
much better way!

Instead, modify the definition of each Heading style to include the Page
Break Before property (in the Modify Style dialog, click Format > Paragraph
Line and Page Breaks tab and check the box). It'll take you about two
minutes to change all the heading styles, and it will automatically apply to
all headings, including new ones you add later.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

Access101

Jay,

Great advice. Just for grins, why is it so slow and clunky, I even turned
off screen updating. Is there a quicker way to not cycle through all the
paras, and just find the "heading..." styled paras?

I'm trying this Modify advice you gave.

Modify Style Format, Paragraph, Line and Page Breaks, Page Break before
OK, OK

I then use Pick formatting to apply, and choose Heading 1, and select all 20
instances. Then I go up to the Toolbar and Apply the Heading 1 style.

What type of page breaks should I be looking for at that point, manual, or
only when it prints?
 
J

Jay Freedman

I'll get to the macro discussion in a minute. First, let's talk about what
you've just done. It sounds like something isn't right.

I assumed (yes, I know...) that you already have applied Heading styles to
some paragraphs, so for example you say there are 20 instances of Heading 1.
Did you follow these steps?

- Click the Format menu and choose Styles and Formatting (or in Word 2007,
press Ctrl+Alt+Shift+S), to display the Styles task pane.
- Right-click the Heading 1 entry in the task pane, or click the down arrow
next to it, and choose Modify to display the Modify Style dialog.
- Click the Format button in the dialog's lower left corner and choose
Paragraph to display the Format Paragraph dialog.
- Click the Line and Page Breaks tab of the dialog.
- Check "Page break before".
- Click OK in each dialog.

If you did all that, every Heading 1 paragraph in the document should
immediately start a new page. You shouldn't have to select all instances or
apply the style or anything else. Then you can repeat the procedure for
Heading 2, etc.

~~~~~~

When you must use a macro to format or change things in various places in a
document that can't be done with a style (and note that this is _not_ one of
those cases), it's far more efficient to use the Replace method of a Range
object than to cycle through all the paragraphs. Depending on what
distinguishing characteristics the targets have, you can search for
formatting (either particular styles or direct formatting), specific text,
or a combination. You may need to use a wildcard search
(http://www.gmayor.com/replace_using_wildcards.htm) if the target contains
variable text. For an example, see
http://word.mvps.org/faqs/macrosvba/SRChangeDateFormat.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

Access101

Jay,

With your help and patience, I figured it out. Thanks, that works great!
All in one fell swoop.

And the wild card searches are going to keep me busy till midnight.

All of it was great, thanks for your time and help.
 

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