问题
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