delete a page

F

Flash Gordon

I amy trying to write a macro that will delete pages 7,9,10 and 12 of a 18
page document. I can see how to get to a page, but then I cannot figure out
the commands to delete specific pages.
 
M

macropod

Hi Flash,

Here's one way:
Sub DeletePages()
Dim i As Integer
Dim MyRange As Range
Set MyRange = ActiveDocument.Range(0, 0)
For i = ActiveDocument.Range.Information(wdActiveEndPageNumber) To 1 Step -1
If i = 7 Or i = 7 Or i = 9 Or i = 10 Or i = 12 Then
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:=i)
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")
MyRange.Delete
End If
Next
End Sub
 
G

Gordon Bentley-Mix

I think you'll find that Word's definition of a "page" is very fluid and can
be influenced by things like the specified printer and finer details of style
definitions, amongst others. In fact, you will note that the Page object is
not a child of the Document object. Instead, it is only related to the
Document object by way of the Pane object through the Window object. In
addition, there is no Delete method for the Page object. This alone should
indicate that the concept of "deleting a page" may not be as simple as one
might think.

However, here are some tips that may assist you in your efforts:

Consider deleting the last page first. The reason for this should be fairly
obvious: as soon as you delete, for example, page 7, page 9 is now page 8,
page 10 is page 9, and so on. Deleting the last page first avoids this
problem.

You may be tempted to select the page you want to delete and then delete the
selection. With only four pages to delete, this would probably be acceptable.
However, it's better practice to define a Range object for the page and then
delete the specified range. This will be quicker and won't cause the screen
to jump around.

In addition, due to the fact that the Page object has no Delete method and
no Range property , you will not be able to work with the Page object
directly. Instead, you will have to use the range of the hidden "\page"
bookmark for each page, as shown in the sample code that macropod has
provided (which actually shows practical application of all the
recommendation outlined above).

Finally, if at all possible, I would look for a way to explicitly define the
content to be deleted - possibly though the use of a series of bookmarks.
This will help to avoid the problems associated with the imprecision inherent
in Word's definition of a page.

BTW, I would be tempted to use a 'Select Case' statement in place of
macropod's 'If' statement, as follows:

<snip>
Select Case i
Case 7, 9, 10, 12
Set MyRange = MyRange.GoTo(What:=wdGoToPage, Name:=i)
Set MyRange = MyRange.GoTo(What:=wdGoToBookmark, Name:="\page")
MyRange.Delete
Case Else
' Do nothing
End Select
<snip>

However, this is just a matter of personal preference in that I find this
construction to be a bit more "descriptive" and simpler to understand, debug
and maintain. A series of 'Or' comparisons, while perfectly acceptable to the
machine, can be a bit more difficult for "wetware" to interpret. (And 'Case'
statements just look prettier IMHO... <g>)

--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
F

Flash

Your definition of page was very en lighting. My objective is to to wind up
with a sub-document that only has specific pages cut/extracted from a master
document--using my past example. Perhaps it would be easier to copy or
"merge" specific pages into a blank document. Maybe I am asking the wrong
question about deleting specific pages. What is the best way to accomplish my
objective. Thanks for your help.
 
M

macropod

Hi Flash,

Making a copy of a document and deleting the unwanted pages from it to make a sub-set, is probably as easy as, if not easier than,
copying the 'wanted' pages to a new file. Apart from not making the copy for you - which you didn't ask for - the code I posted
takes the first course.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top