Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2008

Tip: Looking for answers? Try searching our database.

VBA code to delete a page in a Word doc

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
cyberdude - 17 Feb 2008 13:06 GMT
Hi,

I am using Word 2003.  May I ask how to write a VBA code to delete a
certain page in a Word document?  Thank you.

Mike
Graham Mayor - 17 Feb 2008 13:37 GMT
'Page' is a vague concept in Word, which is not a page layout application.
The nearest you will achieve is

Sub PageDelete()
On Error Resume Next
ActiveDocument.Bookmarks("\page").Range.Delete
End Sub

which will delete the current 'page'.

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Hi,
>
> I am using Word 2003.  May I ask how to write a VBA code to delete a
> certain page in a Word document?  Thank you.
>
> Mike
Greg Maxey - 17 Feb 2008 14:47 GMT
As Graham states "page" in Word is a vague concept.  See:
http://daiya.mvps.org/wordpages.htm

That said, and using Word2003 or later, there are some things that you can
use in addition to the builtin bookmark "\page" that might suit your need.
They are the Panes, Pages, and Rectangle collections primarily used for
defining page setup.

The following example code was tested on a very simple four page document
with text, headers, footers, and a few shapes.  It worked, but as you can
see there are a lot of "ifs" involved so it may be more trouble to set up
that it is worth.

Part of the problem is figuring out which rectangle to delete.  If you
delete the "header" or "footer" rectangle then all the headers in the other
pages are gone as well :-(.  I haven't done any serious testing with these
methods so I don't know what would happen if you delete the maintext
rectangle of a page that is uniquen (i.e., one page lone with different
header and footer than all the other pages.)

Sub DeletePage()
Dim MyPane As Pane
Dim MyPage As Page
Dim i As Long
Dim lngUserView As Long
lngUserView = Application.ActiveWindow.View.Type
Set MyPane = ActiveWindow.ActivePane
On Error GoTo Err_Handler
Set MyPage = MyPane.Pages(InputBox("Enter the page to delete: "))
For i = 1 To MyPage.Rectangles.Count
 If MyPage.Rectangles(i).RectangleType = wdTextRectangle Then
   If MyPage.Rectangles(i).Range.StoryType = wdMainTextStory Then
     MyPage.Rectangles(i).Range.Delete
   End If
 End If
Next i
If Application.ActiveWindow.View.Type <> lngUserView Then
  Application.ActiveWindow.View.Type = lngUserView
End If
Exit Sub
Err_Handler:
If Err.Number = 5894 Then
 Application.ActiveWindow.View.Type = wdPrintView
 Resume
End If
End Sub

Signature

~~~~~~~~~~~~~~~~~~~~~~~~~~~
Greg Maxey -  Word MVP

My web site http://gregmaxey.mvps.org
Word MVP web site http://word.mvps.org
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> 'Page' is a vague concept in Word, which is not a page layout application.
> The nearest you will achieve is
[quoted text clipped - 12 lines]
>>
>> Mike
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.