Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete last page 1

Status
Not open for further replies.

GeoNerd

Technical User
Jul 18, 2006
2
0
0
NL
Hay All,
I'm trying to remove the last page of a word2000 document. (may also be last section). The document (template) holds paragraphs at the end of the document that are used by a macro to construct letters. When the macro finishes, I want to remove those paragraphs.

Found this routine but it only removes the first character of the page.

Thanks for any help...


Here's the code..

Sub DeletePage()
Dim rng1 As Range
Dim lStart As Long
Const pageNo = 3
Set rng1 = ActiveDocument.GoTo(wdGoToPage,wdGoToAbsolute, pageNo)
lStart = rng1.Start
Set rng1 = ActiveDocument.GoTo(wdGoToPage, wdGoToNext, pageNo)
rng1.Start = lStart
rng1.Delete
End Sub
 
This would be MUCH better if those paragraphs were bookmarks.

I do this all the time.

Then when done, to delete them explicitly, it is a simple:
Code:
Sub CleanUp()
ActiveDocument.Bookmarks("ThisPara").Range.Delete
'  etc etc etc for all the appropriate bookmarks
End Sub

You can also do this with removing a Section, but this requires careful coding if you have headers and footers. Word pulls header and footer content backwards. This is absolutely crucial to realize when removing Sections by code.

If you know for sure that all the relevant bookmarks are on the last page, you can use:
Code:
Dim oBM As Word.Bookmark
Selection.EndKey Unit:=wdStory

For Each oBM In ActiveDocument.Bookmarks
("\page") _
.Range.Bookmarks
        oBM.Range.Delete
Next
This simply goes to the end of the document, and deletes every bookmark with the Range of that last page.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top