如何删除vba word中的最后一页

时间:2017-04-30 06:55:37

标签: vba ms-word word-vba

我有一个带有表格对象的word文件。

在我的脚本中,我会多次复制和粘贴,因此会添加页面。

复制并粘贴:

Dim Range2 As Range
Dim r
i_TableAsRange.Copy
Set Range2 = ActiveDocument.Content
Range2.Collapse Direction:=wdCollapseEnd
Range2.Paste

我想删除第二页之后的所有页面。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

改编自Marvin_Guo的帖子here

Sub DeleteAfterPageTwo()

Dim rng As Range
Dim pageCount As Integer

' work out how many pages
pageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)

' set a range from page 3 to the end
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=3
Set rng = Selection.Range
Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=pageCount
rng.End = Selection.Bookmarks("\Page").Range.End

' delete the range
rng.Delete

' optionally remove the left over blank page
Selection.TypeBackspace
Selection.TypeBackspace

End Sub