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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting pages in Word, Need Help. 1

Status
Not open for further replies.

PorscheGT2

Programmer
Jan 20, 2004
23
US
Hello,

Is there a command in VBA that allows to delete pages in Word? The way I'm doing it right now is very clunky; I basically record a macro to position the cursor just right below the page break and do a shift+PageUp to cover the upper pages to delete it. Any help would be greatly appreciated. TIA!

Marvin
 
Unless you have inserted page breaks in your text, pages are dynamically assigned based on the content. That makes it hard to do something with "a page".

Can you make the deletion based on contents instead?



VBAjedi [swords]
bucky.gif
 
Hello VBAJedi,

Thanks for your reply. I do have page breaks inserted in the text. We download these reports from our system and things should be in the same place everytime unless our Data Systems person responsible for generating the reports decides to change something with the formatting. If I have page breaks, is it possible to delete pages then?
 
Hi PorscheGT2,

As VBAjedi says, Word doesn't have a Page object, but it does have commands to move around a page at a time, either using Goto, or the Browser.

For example, this macro will delete the current page (providing it's not the last page in the document - you'll need some extra code to check for that):

Code:
Sub DeletePage()

Selection.GoTo wdGoToPage, wdGoToNext, 1
Start = Selection.Start
Selection.GoTo wdGoToPage, wdGoToPrevious, 1
Range(Selection.Start, Start).Delete

End Sub

If that's not enough to put you on the right track, let us know a bit more precisely what you want.

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
An alternative to deleting the current page, using the reserved Word bookmark "\Page". This will delete all content on the current page regardless of a forced page break or a dynamic one.

NOTE: the range includes the break at the end of the page (if any). However, it does NOT include the final paragraph mark if insertion point is on the last page.

Sub DeletePage()
Dim r As Range
Set r = ActiveDocument.Range( _
Start:=ActiveDocument.Bookmarks("\Page").Range.Start, _
End:=ActiveDocument.Bookmarks("\Page").Range.End)
r.Delete
r = nothing
End Sub
 
Oh, and the above code will delete the last page of a document. It will delete ANY current page. It just does not delete the final paragraph mark of a document.
 
Hi fumei,

Excellent. I'll give you a star for that one.

But the code can be simpler ..

Code:
ActiveDocument.Bookmarks("\Page").Range.Delete

Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top