Consulting

Results 1 to 20 of 20

Thread: Solved: Delete a Page in Word 2003 via VBA?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location

    Solved: Delete a Page in Word 2003 via VBA?

    Hi everyone,

    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.

    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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:[vba]ActiveDocument.Page(2).Delete[/vba] 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.

  3. #3
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  4. #4
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by fumei
    "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:[vba]ActiveDocument.Page(2).Delete[/vba] 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:
    [VBA]
    ActiveDocument.Page(2).Delete
    [/VBA]

    But it naturally doesn't work.

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

    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. 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.



    Quote Originally Posted by TonyJollans
    ....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
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Firstly, sorry, but it should be \Page not /Page.

    Secondly, you can use:

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

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

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Ah, so the 2003 Page object is not really a "page", it is a derived graphical object based on the printer driver?

  7. #7
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    That's how I understand it but it is poorly documented (and that's being generous).
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  8. #8
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi there,

    I tried the code as:

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

    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.
    I give up the request.

    Thanks for all your kindly helps. I so glad.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Erdin? E. Ka
    [vba]
    Sub Thanks2TonyJollansAndGerry()
    ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3).Bookmarks("\Page").Range.Delete
    End Sub
    [/vba]
    Hi Erdin?
    A small tweak to the code.
    Regards
    MD
    [vba]Sub ChangedByMD()
    With ActiveDocument
    .GoTo wdGoToPage, wdGoToAbsolute, 3
    .Bookmarks("\Page").Range.Delete
    End With
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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:
    [VBA]Sub Thanks2TonyJollansAndGerry()
    ActiveDocument.Range.GoTo(wdGoToPage, wdGoToAbsolute, 3).Select
    Selection.Bookmarks("\Page").Range.Delete
    End Sub[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  12. #12
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Wierd, isn't it? I think this is one of those cases where you have to use the Selection.

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

    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  13. #13
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi everyone,

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

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

    Thank you veryyyyyyyyyyyyy much.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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:[vba] With ActiveDocument
    .GoTo wdGoToPage, wdGoToAbsolute, 3
    .Bookmarks("\Page").Range.Delete
    End With [/vba]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:[vba]ActiveDocument.GoTo wdGoToPage, wdGoToAbsolute, x[/vba]while a valid instruction, means nada, zip, nothing.

    Ah...Word.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    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.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Well I tried my code again and can't get it to work! How about
    [VBA] Selection.GoTo wdGoToPage, wdGoToAbsolute, 3
    ActiveDocument.Bookmarks("\Page").Range.Delete
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, that will work, as you are using the Selection.

  18. #18
    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

  19. #19
    aha..
    if i always start deleteing the last one first

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Aha indeed. Always try and do deletions from any collection backwards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •