Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Using VBA
    I have a 7 page document. The user has six options but three of those options would require that pages 3 and 4 to be deleted and the page numbers repaginated

    Is it possible to delete a whole page from the document ?

    so its a bit like this
    options 1-6 available
    options 1-3 require that page 3 and 4 of the document are removed
    options 4-6 leave the pages as they are

    John

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 15 Times in 15 Posts
    I'd recommend making pages 3 and 4 a separate section by inserting section breaks before and after them (i.e. a section break at the end of page 2 and another one at the end of page 4).
    Assuming that you have no other section breaks, the document would then contain 3 sections:
    Section 1: pages 1 and 2
    Section 2: pages 3 and 4
    Section 3: pages 5, 6 and 7

    To delete section 2, you can use

    ActiveDocument.Sections(2).Range.Delete

    Word should adjust the page numbers automatically

  4. #3
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Dallas, Texas, USA
    Posts
    111
    Thanks
    2
    Thanked 5 Times in 5 Posts
    John,

    Unforttunately, as Slinky suggests, the only way to delete a whole page from a Word document is to make it into a Section. This is because there is no such thing as a Page in the object hierarchy of a Word document. There are only two contexts in which the term "page" has meaning.

    1) Print Layout view.

    2) A page formatted for a specific printer.

    The Document hierarchy is composed of the following elements.

    1) Document
    2) Stories (main, footnotes, endnotes, TOC, etc)
    3) Sections
    4) Paragraphs
    5) Characters

    Such things as Page Layout, which might lead you to think there is such a thing as a Page object, are properties of a Section.

    I have fought the same battle from time to time since I started writing VBA for WinWord in 1996, and I don't recall that I've ever found a way to identify the page of which, e. g., a paragraph, is a part.
    David Gray, Chief Wizard
    WizardWrx
    Irving, Texas, USA

    WizardWrx Web - Technical Articles and Free Software
    You are more important than any technology we may employ.

  5. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 27 Times in 26 Posts
    Quote Originally Posted by David Gray View Post
    I have fought the same battle from time to time since I started writing VBA for WinWord in 1996, and I don't recall that I've ever found a way to identify the page of which, e. g., a paragraph, is a part.
    You can also get page number with code like:

    Selection.Paragraphs(1).Range.Information wdActiveEndPageNumber

    (but that doesn't always tell the whole story, for example when a paragraph starts on one page and ends on another)

    Gary

  6. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,776
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Quote Originally Posted by jonrpullen View Post
    Using VBA
    I have a 7 page document. The user has six options but three of those options would require that pages 3 and 4 to be deleted and the page numbers repaginated

    Is it possible to delete a whole page from the document ?

    so its a bit like this
    options 1-6 available
    options 1-3 require that page 3 and 4 of the document are removed
    options 4-6 leave the pages as they are

    John
    Hi John,

    I think you'll eventually run into problems with this approach. For example, if the user selects one or more of options 1-3 and pages 3 & 4 get deleted, how do you propose to get them back if the user decides he/she should have selected one or more of options 4-6 instead? A better approach is to insert a bookmark or a conditional field where the variable text should go, and modify the bookmark's contents/field display according to the option(s) selected. The bookmark route is perhaps the one to use if there is no special formatting for any of the variable text; otherwise I'd recommend a conditional field.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  7. #6
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Dallas, Texas, USA
    Posts
    111
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Gary,

    Quote Originally Posted by Gary Frieder View Post
    You can also get page number with code like:

    Selection.Paragraphs(1).Range.Information wdActiveEndPageNumber

    (but that doesn't always tell the whole story, for example when a paragraph starts on one page and ends on another)

    Gary

    I thought about that, for about a nanosecond, and decided against mentioning it, for the reasons you gave that might make the "information" unreliable.

    My guess is that the best approach is to divide the document into sections.
    David Gray, Chief Wizard
    WizardWrx
    Irving, Texas, USA

    WizardWrx Web - Technical Articles and Free Software
    You are more important than any technology we may employ.

  8. #7
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,776
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Hi David,

    Working with Sections doesn't really address the problem of how one might restore the 'missing' material once it's been deleted by accident. Granted, on could use a dedicated Section instead of a bookmark, but you'd still need a mechanism for restoring the actual content.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #8
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Dallas, Texas, USA
    Posts
    111
    Thanks
    2
    Thanked 5 Times in 5 Posts
    I don't think anybody has mentioned the possibility of hiding the text, rather than deleting it.
    David Gray, Chief Wizard
    WizardWrx
    Irving, Texas, USA

    WizardWrx Web - Technical Articles and Free Software
    You are more important than any technology we may employ.

  10. #9
    Lounger
    Join Date
    Oct 2008
    Location
    London
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think hiding ther text would leave you printing a couple of blank pages
    For my project it was good enough to deletethe section as there was no liklihood of it being returned to the document as it was a situation of two track to take one deleted the text the other kept it

    Thanks for the assistance David it was interesting but a pity that a page is not there

  11. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    3,776
    Thanks
    0
    Thanked 162 Times in 150 Posts
    Quote Originally Posted by David Gray View Post
    I don't think anybody has mentioned the possibility of hiding the text, rather than deleting it.
    Hi David,

    Again, the approach is problematic. Unless you can control the end-users' hidden text:
    1. display settings, the hidden text may appear little changed on screen
    2. print settings, the hidden text may still be printed.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  12. #11
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Dallas, Texas, USA
    Posts
    111
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Paul,

    I think it's fair to say that there are probalby no "perfect" solutions.

    As I read your response, yet another idea occurred to me, but it involves a feature with which I have zero experience - versioning.
    David Gray, Chief Wizard
    WizardWrx
    Irving, Texas, USA

    WizardWrx Web - Technical Articles and Free Software
    You are more important than any technology we may employ.

Posting Permissions

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