简体   繁体   English

VBA (Word) 如何在段落处插入分页符?

[英]VBA (Word) How to insert a Page Break at Paragraph?

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谢谢朱利叶斯

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.在任何情况下,关键是使用特定的Range对象。 It's not possible to "collapse" the entire 15th paragraph - the method has no action.不可能“折叠”整个第 15 段 - 该方法没有任何动作。 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.段落Range需要分配给一个独立的Range对象,可以折叠,然后在那个Range对象处插入分页符。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM