none
Extracting pages RRS feed

  • Question

  • Hi

    I have a three page word document. How can I delete the third (last page) from the document via code? Or alternatively extract the first two pages into a separate document.

    Thanks

    Regards



    • Edited by Y a h y a Monday, February 13, 2017 4:13 PM
    Monday, February 13, 2017 1:31 PM

Answers

  • Hi Y a h y a,

    Refer code below to delete page number 3 from document.

    Sub demo()
    Dim Rng As Range, pgno As Long
    pgno = 3
    With ActiveDocument
      Set Rng = .GoTo(What:=wdGoToPage, Name:=pgno)
      Set Rng = Rng.GoTo(What:=wdGoToBookmark, Name:="\page")
      Rng.Delete
    End With
    
    End Sub

    Reference:

    Default simpler way to delete a particular page in a Word document???

    below is the code to copy first 2 page to new document.

    Sub demo2()
    Dim rgePages As Range
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=1
    Set rgePages = Selection.Range
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2
    rgePages.End = Selection.Bookmarks("\Page").Range.End
    rgePages.Select
    rgePages.Copy
    Dim wd As Document
    Set wd = Documents.Add
    wd.Activate
    Selection.Paste
    
    
    End Sub
    

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 14, 2017 4:07 AM
    Moderator