简体   繁体   English

使用 VBA 在 MS Word 文档中添加页面会添加额外的页面

[英]Adding a page in MS Word document using VBA adds extra pages

I have MS word file with macro having table in the first page.我有带有宏的 MS word 文件,第一页中有表格。 (Table is created using labels). (表是使用标签创建的)。

But When cells in table is not sufficient I want to add another page, with the similar table in it.但是当表格中的单元格不够用时,我想添加另一个页面,其中包含类似的表格。

So I am trying to code as below所以我试图编码如下

ActiveDocument.Sections(1).Range.Copy ActiveDocument.Sections(1).Range.Copy

ActiveDocument.Sections.Add.Range.Paste ActiveDocument.Sections.Add.Range.Paste

But this code some time changes the format of first page.但是这段代码有时会改变第一页的格式。 And also create third blank page with second page having content of first.并创建第三个空白页,第二页的内容为第一页。

Can any body let me know how to add only a page having same content of first page, without changes in first page.任何正文都可以让我知道如何仅添加与第一页内容相同的页面,而不更改第一页。

Thanks谢谢

The table will flow into the next page automatically, and you can specify repeating heading rows as well.表格会自动进入下一页,您也可以指定重复的标题行。

But assuming you need a new table...但假设你需要一张新桌子......

Your original question needs some clarifications (see some points below), but see if the following code spoils your formatting:您的原始问题需要一些说明(请参阅下面的一些要点),但请查看以下代码是否会破坏您的格式:

Sub copytable1()
Dim rtarget As Word.Range
With ActiveDocument
  Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
  rtarget.InsertBreak Type:=Word.WdBreakType.wdPageBreak
  Set rtarget = .Range(.Content.End - 1, .Content.End - 1)
  rtarget.FormattedText = .Tables(1).Range.FormattedText
  Set rtarget = Nothing
End With
End Sub

It assumes that它假设

  • the table you want to copy is the first table in the document您要复制的表格是文档中的第一个表格
  • you just want to insert a new page at the end of the document and insert a copy of the table after that.您只想在文档末尾插入一个新页面,然后在此之后插入表格的副本。 If your document can already be multi-page, you would need significantly different code.如果您的文档已经可以是多页的,您将需要明显不同的代码。

When creating editable Word documents, as a "good practice," you would use a paragraph style with the "Page Break Before" property set to achieve the break, rather than a hard page break as I have done.创建可编辑的 Word 文档时,作为“良好做法”,您将使用设置了“分页前”属性的段落样式来实现分页,而不是像我所做的那样使用硬分页。

Alternatively,或者,

Sub copytable3()
Dim rtarget As Word.Range
With ActiveDocument
  Set rtarget = .Range
  rtarget.Collapse wdCollapseEnd
  rtarget.InsertParagraphAfter
  Set rtarget = .Range
  rtarget.Collapse wdCollapseEnd
  rtarget.ParagraphFormat.PageBreakBefore = True
  Set rtarget = .Range
  rtarget.Collapse wdCollapseEnd
  rtarget.InsertParagraphAfter
  Set rtarget = .Range
  rtarget.Collapse wdCollapseEnd
  rtarget.ParagraphFormat.PageBreakBefore = False
  Set rtarget = .Range
  rtarget.Collapse wdCollapseEnd
  rtarget.Select
  rtarget.FormattedText = .Tables(1).Range.FormattedText
  Set rtarget = Nothing
End With
End Sub

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

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