Copying a Range of Pages in a Macro

Written by Allen Wyatt (last updated February 6, 2024)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

Kevin wonders if there is a way, in VBA, to copy a range of pages. He knows how to copy paragraphs and a contiguous range of text, but he doesn't know how to copy entire pages.

There is no VBA object that references an entire page, such as there is with a paragraph, nor is there a VBA statement that will allow you to select a page. You can, however, calculate what would comprise a page and then select that range. The general concept is shown in this macro:

Sub CopyPages1()
    Dim rCopy As Range

    Set rCopy = ActiveDocument.GoTo(What:=wdGoToPage, _
      Which:=wdGoToAbsolute, Count:=7)
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9

    rCopy.End = Selection.Bookmarks("\Page").Range.End

    rCopy.Select
End Sub

This macro sets the rCopy range equal to the point where page 7 begins. It then selects the text at the very beginning of page 9 and uses one of Word's built-in bookmarks to select from the top of page 7 to the end of page 9. (That is what this macro is designed to do—select pages 7 through 9.)

You should note, however, that pages are very fluid in Word. If you use the exact same document on two different systems, the above macro may very well return different selections entirely. Why? Because the pages can flow differently on each of the systems.

If you want to actually copy the selected pages (7 through 9), then you can modify the macro just a bit to get the desired result.

Sub CopyPages2()
    Dim rCopy As Range
    Dim rCurrent As Range

    Set rCurrent = Selection.Range

    Set rCopy = ActiveDocument.GoTo(What:=wdGoToPage, _
      Which:=wdGoToAbsolute, Count:=7)
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=9

    rCopy.End = Selection.Bookmarks("\Page").Range.End
    rCopy.Copy
    Documents.Add
    ActiveDocument.Range.PasteSpecial

    rCurrent.Select
End Sub

Note that the macro "remembers" what the user had selected before it was run, storing the range in the rCurrent variable and using it to again select that area at the end of the macro. The selected pages (again, 7 through 9) are copied to a brand new document.

You should note that the above macros only work properly if there are actually at least 9 pages in the current document. If you would like a more flexible and robust macro, you might consider the following variation.

Sub CopyPages3()
    Dim rCopy As Range
    Dim rCurrent As Range
    Dim sTemp As String
    Dim i As Integer
    Dim iStart As Integer
    Dim iEnd As Integer
    
    Set rCurrent = Selection.Range

    ' Get page numbers to be copied
    sTemp = InputBox("Page range to copy (use format 6-7)", "")
    i = InStr(sTemp, "-")
    If i > 0 Then
        iStart = Val(Left(sTemp, i - 1))
        iEnd = Val(Mid(sTemp, i + 1))
        
        If iStart < 1 Then iStart = 1
        If iEnd < iStart Then iEnd = iStart
        With ActiveDocument.Range
            If iStart > .Information(wdNumberOfPagesInDocument) Then
                iStart = .Information(wdNumberOfPagesInDocument)
            End If
            If iEnd > .Information(wdNumberOfPagesInDocument) Then
                iEnd = .Information(wdNumberOfPagesInDocument)
            End If
        End With

        ' Set the range
        Set rCopy = ActiveDocument.GoTo(What:=wdGoToPage, _
          Which:=wdGoToAbsolute, Count:=iStart)
        
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, _
          Count:=iEnd
        rCopy.End = Selection.Bookmarks("\Page").Range.End
        
        ' Copy range to a new document
        rCopy.Copy
        Documents.Add
        ActiveDocument.Range.PasteSpecial

        rCurrent.Select
    Else
        If sTemp > "" Then
            MsgBox "There is no dash character"
        End If
    End If
End Sub

This version asks the user to specify a page range to be copied. The page numbers are adjusted to make sure that the starting number is less than or equal to the ending number, and both starting and ending numbers are ensured to be less than the total number of pages in the document.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (5295) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Line Breaks After a Slash

Some writers use the slash to combine words and as shorthand to signify "or" or "and." This, of course, makes for some ...

Discover More

Updating Links in Copied Files

When you copy workbooks that contain links, you may be at a loss as to how to update those links. There are a couple of ...

Discover More

Pulling Access Information into Excel

If you have a lot of data stored in Access databases, you may want to get at that information using Excel. There are a ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Dissecting a String

Want to pull a string apart in a macro? It's easy using the string functions introduced in this tip.

Discover More

Accessing Paragraphs in a Macro

Need to process a document, paragraph by paragraph, in a macro? It's easy to do once you understand that Word's object ...

Discover More

Changing the Format of Existing Dates

There are a myriad of ways in which a date can be formattedâ€"day first, month first, number of digits in the year, etc. ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four less than 7?

2021-12-04 02:02:03

Brenton

Hi I was just wondering if this tip has worked for other people.

I am trying it however I can’t seem to get the footer and header to copy over just the content


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.