0

I am trying to insert a page break at a specific paragraph, but it always inserts the page break at the first line/paragraph of the document, I don't understand why. Any ideas?

With Word.ActiveDocument
    .Paragraphs(15).Range.Collapse Direction:=wdCollapseEnd
    .Paragraphs(15).Range.InsertBreak WdBreakType.wdPageBreak
    'or this way (Is there a differece): .Paragraphs(15).Range.InsertBreak Type:=wdPageBreak
End With

Thanks Julius

0

1 Answer 1

1

I see something different: for me, it replaces the specified paragraph with the page break, as described in the Help topic.

In any case, the key to this is to work with a specific Range object. It's not possible to "collapse" the entire 15th paragraph - the method has no action. The paragraph Range needs to be assigned to an independent Range object, which can be collapsed, then the page break inserted at that Range object.

For example:

Sub TestInsertPageBreak()
    Dim paraRange As Word.Range

    With ActiveDocument
        Set paraRange = .Paragraphs(15).Range
        paraRange.Collapse Direction:=wdCollapseEnd
        paraRange.InsertBreak WdBreakType.wdPageBreak
    End With
End Sub
Sign up to request clarification or add additional context in comments.

4 Comments

So, it didn't work for me with the range object, but if insert the break before I input my text and my pictures, it works. Although I don't understand the paragraph numbering: My paragraph(15) does not refer to the 15th paragraph in my document, at least if I let VBA count the paragraphs to that point, it says a different number. However, the range object does provide some easy to the whole code! Thanks.
@Heka I'm not sure I understand parts of your comment, but... I took the Paragraphs(15) directly from the code in the question... I did test what I posted before copying it in, here, and it worked "as advertised", inserting the page break after the "target paragraph". Without seeing your document, I can't know what might influence the paragraph count. But if there's a table, every cell in the table will be counted as (at least) one paragraph...
I am not sure either, what this "influence" is. I can post the things I did later. Your code works (that's why I marked it as solution), but not if I insert pictures before inserting the page break. I will edit the original post later!
@Heka Pictures? Ah... If that's an issue, please compose the exact case as a NEW question, with steps to reproduce what you're doing. VERY IMPORTANT include whether the pictures are in-line with the text (InlineShape objects) or have text wrap formatting (Shape) objects as that influences how the code has to work. Screen shots with the desired result and what the code actually does would be helpful, as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.