PDA

View Full Version : Solved: Delete a Page in Word 2003 via VBA?



Erdin? E. Ka
11-26-2006, 03:35 PM
Hi everyone,:hi:

I want to learn how to delete a particular page in Word 2003 via VBA?

For example, how must be my VBA code; if i want to delete 2th page of the document?

Thanks in advance.

:friends:

fumei
11-27-2006, 05:36 AM
"Page" does not really exist in Word except as a dynamic range.

I do not have Word 2003, but I know there is a Page object that is not in earlier object models. However, I do not think there is a Page collection.

There is nothing that says THAT is page 2. Word can say THAT is page 2, but only after it paginates the document. It has to figure out what is page 2. So...say it does so. You add text to page 1. This pushes text into page 2. Well, now page 2 is different. It is a different page 2 from what it was before.

So what is page 2? What it is NOW, or what it was before? If Word has repaginated...then page 2 is what it is now. If Word has NOT repaginated, page 2 is what it was before.

Since Word is normally repaginating, it should be the current range.

Even if you make hard page breaks to keep text on one page, again, if text is inserted before that page, then the page number will go up.

I am not familiar with the Page object in 2003. It may have a .Delete method. In which case, a .Delete would delete the range of the page. Something like:ActiveDocument.Page(2).Delete Is this a valid instruction?

Other than that, you have to determine what IS page 2 first, then delete it. Which of course can be done.

TonyJollans
11-27-2006, 08:10 AM
Gotta be quick at the moment - the Pages Collection does not have a Delete method - it is a graphical object representing the printed layout. The way to delete pages is to use the built-in "/Page" bookmark.

Erdin? E. Ka
11-27-2006, 08:54 AM
"Page" does not really exist in Word except as a dynamic range.

I do not have Word 2003, but I know there is a Page object that is not in earlier object models. However, I do not think there is a Page collection.

There is nothing that says THAT is page 2. Word can say THAT is page 2, but only after it paginates the document. It has to figure out what is page 2. So...say it does so. You add text to page 1. This pushes text into page 2. Well, now page 2 is different. It is a different page 2 from what it was before.

So what is page 2? What it is NOW, or what it was before? If Word has repaginated...then page 2 is what it is now. If Word has NOT repaginated, page 2 is what it was before.

Since Word is normally repaginating, it should be the current range.

Even if you make hard page breaks to keep text on one page, again, if text is inserted before that page, then the page number will go up.

I am not familiar with the Page object in 2003. It may have a .Delete method. In which case, a .Delete would delete the range of the page. Something like:ActiveDocument.Page(2).Delete Is this a valid instruction?

Other than that, you have to determine what IS page 2 first, then delete it. Which of course can be done.


Thank you very much to kindly Gerry,

You are right Gerry there is a Page collection.
I tried before send my message as:

ActiveDocument.Page(2).Delete


But it naturally doesn't work. :yes

Alos i tried other one : (One page has 50 rows in my document.)
ActiveDocument.Range(1, 50).Delete

But this time: I had different result per running.
One time only deleted 20 rows.
One time only deleted 30 rows.
There is no logical tip for me. :think: Just a little complicated.

Anyway, i researched for "row". But i think that depends on "creating a table on the document".

So i think i should find a different way to solve it.




....The way to delete pages is to use the built-in "/Page" bookmark.

Thank you very much to kindly Tony,

I am inexperienced about Word. Could you give me a example about the built-in "/Page" bookmark.?

Thanks for all hepls again.

Best regards.

Erdin

TonyJollans
11-27-2006, 10:04 AM
Firstly, sorry, but it should be \Page not /Page.

Secondly, you can use:


ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3) _
.Bookmarks("\Page").Range.Delete


If the Rows you talk about are Table Rows and the Tables span Pages, however, it may be more complicated.

fumei
11-27-2006, 11:02 AM
Ah, so the 2003 Page object is not really a "page", it is a derived graphical object based on the printer driver?

TonyJollans
11-27-2006, 12:02 PM
That's how I understand it but it is poorly documented (and that's being generous).

Erdin? E. Ka
11-27-2006, 07:37 PM
Hi there,

I tried the code as:


Sub Thanks2TonyJollansAndGerry()
ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3).Bookmarks("\Page").Range.Delete
End Sub


But the code returned me an error message as:

Run-time error '5941':
The requested member of the collection does not exist.

So, probably my request is irrelevant to Word 2003 Application. :think:
I give up the request.

Thanks for all your kindly helps. I so glad. :hi:

mdmackillop
11-28-2006, 01:23 AM
Sub Thanks2TonyJollansAndGerry()
ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3).Bookmarks("\Page").Range.Delete
End Sub


Hi Erdin?
A small tweak to the code.
Regards
MD
Sub ChangedByMD()
With ActiveDocument
.GoTo wdGoToPage, wdGoToAbsolute, 3
.Bookmarks("\Page").Range.Delete
End With
End Sub

TonyJollans
11-28-2006, 02:36 AM
I *think* the \Page bookmark may only work properly with the Selection, especially if there are tables in the document, but I have never managed to pin it down. Try this:
Sub Thanks2TonyJollansAndGerry()
ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3).Select
Selection.Bookmarks("\Page").Range.Delete
End Sub

fumei
11-28-2006, 05:03 AM
Actually Tony, there is something odd with this code. I too had 5941 error. But not consistently. I tried it again. 5941 error. Tried it again. It ran fine. Tried it again. Ran fine. Tried it again. 5941 error.

Not sure why that would be.

TonyJollans
11-28-2006, 06:06 AM
Wierd, isn't it? I think this is one of those cases where you have to use the Selection.

Sub Thanks2TonyJollansAndGerry()
Selection.GoTo(wdGoToPage, wdGoToAbsolute, 3).Bookmarks("\Page").Range.Delete
End Sub

What documentation there is seems to be all about the Selection but doesn't specifically say it doesn't work with non-selected Ranges. As the built-in bookmarks are not in any collection, and dynamic by their nature, there must be special case code dealing with them - apparently not special enough.

Erdin? E. Ka
11-28-2006, 06:08 AM
Hi everyone,

I tried your codes a lot times on Word 2003. :type ( ChangedByMD And Edited Thanks2TonyJollansAndGerry ). There is no any error. :yay:yay

These are perfect and perfect and perfect again.!!!!

Thank you veryyyyyyyyyyyyy much.:2jump:

fumei
11-28-2006, 08:25 AM
The \Page predefined bookmark is named as:
Current page, including the break at the end of the page, if any. The current page contains the insertion point.
So....if the insertion point is NOT on the page defined by the Range it does not work. 5941 error. So, yes, it is tied to the Selection.

On the other hand, Malcolm's code: With ActiveDocument
.GoTo wdGoToPage, wdGoToAbsolute, 3
.Bookmarks("\Page").Range.Delete
End With never gives an error, BUT - for me at least - always deletes the page the Selection IS on, regardless of what page that is. Which actually makes sense, as there are TWO instructions.

1. .GoTo wdGoToPage, wdGoToAbsolute, 3

2. .Bookmarks("\Page").Range.Delete

Now, #2 is an explicit instruction to the Selection, or rather the current page the insertion point is on. As \Page is tied to that, the current page is deleted, which is what happens. No matter what page it is on.

The question though is...what the heck is happening with:

.GoTo wdGoToPage, wdGoToAbsolute, 3

There is no error, but as far as I can see nothing is actioned by the GoTo. Help states that GoTo can apply to the Document...but what the heck does THAT mean????

A Range GoTo - sure.
A Selection GoTo - sure.

As far as I can see, while Malcolm's code has no error, having the Document GoTo has no effect really. His code deletes the page the Selection is on. Maybe something is "gone" to page 3 (absolute), but I have no idea what it is that has gone there. It is not the Selection, and if there has been no Range object defined...WHAT has been actioned by the GoTo?

I have tried it numerous times, trying to see what happens, but it sure looks like:ActiveDocument.GoTo wdGoToPage, wdGoToAbsolute, xwhile a valid instruction, means nada, zip, nothing.

Ah...Word.

TonyJollans
11-28-2006, 09:15 AM
GoTo is a Method of the Document which returns a Range.

When run as a Function, Word goes away and determines the Range but it is then discarded so, yes, in effect it does nothing.

mdmackillop
11-28-2006, 11:36 AM
Well I tried my code again and can't get it to work! How about
Selection.GoTo wdGoToPage, wdGoToAbsolute, 3
ActiveDocument.Bookmarks("\Page").Range.Delete

fumei
11-29-2006, 08:05 AM
Yes, that will work, as you are using the Selection.

Sirfanta
05-31-2007, 01:33 AM
Hi everyone =)

This helpt me a lot. But i have a problem :(
I have a word doc with 60pages. The user decideds what is going to be in the doc, by checking CheckBoxes.
I want to delete the rest of the documents that are not checkt out.

Problem with :

Selection.GoTo wdGoToPage, wdGoToAbsolute, 3
ActiveDocument.Bookmarks("\Page").Range.Delete
is that for every page i delete the page nr is change.

Do u have any god tips?

Best regards.
Trond

Sirfanta
05-31-2007, 03:26 AM
aha..
if i always start deleteing the last one first :D

fumei
06-01-2007, 01:34 PM
Aha indeed. Always try and do deletions from any collection backwards.