How delete the last page in vba word

微笑、不失礼 提交于 2020-07-09 15:58:27

问题


I have a word file with table object.

In my script I copy and paste many times and as a result pages are added.

Copy and paste:

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

I want to delete all pages after the second page.

How can I do that?


回答1:


Adapted from Marvin_Guo's post 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


来源:https://stackoverflow.com/questions/43703811/how-delete-the-last-page-in-vba-word

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!