Using wdPageBreak without losing the selected Paragraph.

G

Guest

I am currently trying to write a macro to search for a particular style in a
Word document, and every time it is encountered insert a page break.
This seemed failry straightforwards. However, the problem I am having is
that the text contained in the Style is being replaced by the page break, so
I am getting page breaks without retaining the original paragraphs.

I obviously want to have both the page breaks and the paragraphs I have
searched for present.

Below is my code. Any suggestions much appreciated. I basically need a way
of making the Range the cursor input to the paragraph rather than the whole
paragraph itself and I am stuck as to how to do this.


Public Function InsertPageBreak()

Dim para As Word.Paragraph

For Each para In e3Doc.Paragraphs
If para.Style = "ScheduleTitle" Then
para.Range.InsertBreak Type:=wdPageBreak
End If
Next para
End Function

Any help very much appreciated. Adam
 
D

Daiya Mitchell

Instead of a macro, cannot you define the style to have a Page Break Before?
 
G

Guest

No unfortunately, we need to do it be a macro. Part of the service the
company I work for is Document Automation, and unfortuantely this client
wants a physical break by a macro rather than page break as part of the
formatting.

This is because if you hit return at the start of a style witha page break
you get another page break, which they do not want. They want the breaks to
be disassociated with the styles, hence we have to use a macro.

Any help much appreciated. Adam
 
D

Daiya Mitchell

In that case, you would probably get a quicker answer if you post in a group
with word.vba or word programming in the name. Much higher proportion of
people knowledgeable about macros in those groups, though there are some
here.
 
G

Greg

ad,

How about something like:

Sub InsertPageBreak()
Dim oPara As Word.Paragraph
Dim myRange As Range
For Each oPara In ActiveDocument.Paragraphs
If oPara.Style = "Heading 1" Then
Set myRange = oPara.Range.Duplicate
myRange.Collapse Direction:=wdCollapseEnd
myRange.MoveEnd Unit:=wdCharacter, Count:=-1
myRange.InsertBreak Type:=wdPageBreak
End If
Next oPara
End Sub

Does your company pay good?
 

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