none
How to copy and insert a page in MS Word

    Question

  • I am not sure if this is the best place to post this comment, but here goes.

    I am setting up a document in MS Word 2007 and would like to have a checkbox at the bottom of a certain page that is enabled when true to insert a page break, copy the page above and then insert the copied page into the new page that has just been created.

    I have got the checkbox in from the developer tab, opened up the Microsoft Visual Basic editor and got it to insert a page break when it is selected (checked) giving the new page.

    (This is where I am now stuck) I would like to know where best to find some code, or to ask for code that copies that page above and inserts it into the new page.

    Any help or thoughts on the matter are really apprecaited.  
    Monday, September 07, 2009 3:50 AM

Answers

  • Assuming the page to be copied is exactly one page above the new one created and the selection point when your code is run is on the new page, then this should work:

    'Go back one page
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1
    'Copy current page
    ActiveDocument.Bookmarks("\Page").Range.Copy
    'Go forward one page
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    'Paste copied page
    Selection.Paste

    If you need to goto/copy a specific page you can use 'Name'. E.g

    Selection.GoTo What:=wdGoToPage, Name:="3"

    would go to page 3.
    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Monday, September 07, 2009 1:28 PM
  • Do you mean that the checkbox will insert a section break rather than a page break?  And that the page that you then need to copy is protected?

    If you are using section breaks, then you need wdGoToSection rather than wdGoToPage. If you insert a section break and the section (i.e. the page above) that you are trying to copy is protected meaning it can't be copied, then your code can unprotect it before you copy it. You can then re-protect the section.

    The following would work in that scenario but not sure if that is what you mean.

    Dim wasProtected As Boolean
    Dim SectionAbove As Integer
    'SectionAbove = current section minus one
    SectionAbove = CInt(Selection.Information(wdActiveEndSectionNumber)) - 1
    'See if section above was protected, and unprotect
    If ActiveDocument.Sections(SectionAbove).ProtectedForForms = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = False
        wasProtected = True
    End If
    'Go back one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, Count:=1
    'Copy current page
    ActiveDocument.Bookmarks("\Page").Range.Copy
    'Go forward one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1
    'Paste copied page
    Selection.Paste
    'Reprotect if section was protected
    If wasProtected = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = True
    End If


    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Tuesday, September 08, 2009 9:02 AM
  • If the doc is being password protected then you can always unprotect the document at the start of your code/sub. E.g, 

       If ActiveDocument.ProtectionType <> wdNoProtection Then
          ActiveDocument.Unprotect("mypassword")
       End If

    Then reprotect it at the end of your code/sub

      ActiveDocument.Protect Password:="mypassword", NoReset:=True, Type:=2

    Use your password in place of mypassword.

    You won't need the "ActiveDocument.Sections(SectionAbove).ProtectedForForms " code if you are password protecting and unprotecting the whole document
    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Wednesday, September 09, 2009 6:40 PM

All replies

  • Assuming the page to be copied is exactly one page above the new one created and the selection point when your code is run is on the new page, then this should work:

    'Go back one page
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToPrevious, Count:=1
    'Copy current page
    ActiveDocument.Bookmarks("\Page").Range.Copy
    'Go forward one page
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Count:=1
    'Paste copied page
    Selection.Paste

    If you need to goto/copy a specific page you can use 'Name'. E.g

    Selection.GoTo What:=wdGoToPage, Name:="3"

    would go to page 3.
    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Monday, September 07, 2009 1:28 PM
  • Hi JFK555,


    That is awesome - thank you!

    thank you for getting back to me with that code - I have put it in and it does the trick - really appreciate that!


    I have a question about protecting the document - if I do protect the document, and section break the page above that is being copied and the area around the checkbox, so that it can be selected (not protected), is there a way to get the code to still copy the page above into the new page below?


    I tried to do it, and it does not copy the page above to the new page below, but copies / moves it to the next page while moving the page after up.


    Looking forward to your reply / response -


    Regards




    Tuesday, September 08, 2009 7:59 AM
  • Do you mean that the checkbox will insert a section break rather than a page break?  And that the page that you then need to copy is protected?

    If you are using section breaks, then you need wdGoToSection rather than wdGoToPage. If you insert a section break and the section (i.e. the page above) that you are trying to copy is protected meaning it can't be copied, then your code can unprotect it before you copy it. You can then re-protect the section.

    The following would work in that scenario but not sure if that is what you mean.

    Dim wasProtected As Boolean
    Dim SectionAbove As Integer
    'SectionAbove = current section minus one
    SectionAbove = CInt(Selection.Information(wdActiveEndSectionNumber)) - 1
    'See if section above was protected, and unprotect
    If ActiveDocument.Sections(SectionAbove).ProtectedForForms = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = False
        wasProtected = True
    End If
    'Go back one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, Count:=1
    'Copy current page
    ActiveDocument.Bookmarks("\Page").Range.Copy
    'Go forward one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1
    'Paste copied page
    Selection.Paste
    'Reprotect if section was protected
    If wasProtected = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = True
    End If


    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Tuesday, September 08, 2009 9:02 AM
  • Hi JFK555,


    again, thank you for the code that you have put up here -


    I have tried it, and when the document is not protected it still works, copies the page above to the new page and moves the other pages down.

    I have adjusted the section count so that it works to how the document is laid out, and runs with the correct outcome - copying the page to a new page.

    When I protect the document, the error message that I get is:

    Run time error 5887
    Command not available because document is passowrd protected.

    Then the option to debug. When I click to debug, this is what comes up: (the line of code that is highlighted in the debug code window is underlined below):



    Private Sub Document_New()

    End Sub

    Private Sub InsertNewPage_Click()


    Dim wasProtected As Boolean
    Dim SectionAbove As Integer
    'SectionAbove = current section minus one
    SectionAbove = CInt(Selection.Information(wdActiveEndSectionNumber)) - 2
    'See if section above was protected, and unprotect
    If ActiveDocument.Sections(SectionAbove).ProtectedForForms = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = False
        wasProtected = True
    End If
    'Go back one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToPrevious, Count:=6
    'Copy current page
    ActiveDocument.Bookmarks("\Page").Range.Copy
    'Go forward one section
    Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=5
    'Paste copied page
    Selection.Paste
    'Reprotect if section was protected
    If wasProtected = True Then
        ActiveDocument.Sections(SectionAbove).ProtectedForForms = True
    End If


    End Sub


    Please let me know if you think it is possible to get it to copy the page over when it is protected - and if there is some code for this I thank you in advance for any effort put into trying to work it out;

    If you require any more information please let me know;

    Regards


    Wednesday, September 09, 2009 7:06 AM
  • If the doc is being password protected then you can always unprotect the document at the start of your code/sub. E.g, 

       If ActiveDocument.ProtectionType <> wdNoProtection Then
          ActiveDocument.Unprotect("mypassword")
       End If

    Then reprotect it at the end of your code/sub

      ActiveDocument.Protect Password:="mypassword", NoReset:=True, Type:=2

    Use your password in place of mypassword.

    You won't need the "ActiveDocument.Sections(SectionAbove).ProtectedForForms " code if you are password protecting and unprotecting the whole document
    • Marked as answer by Tim Li Monday, September 14, 2009 6:27 AM
    Wednesday, September 09, 2009 6:40 PM
  • Hi JFK555,


    I have put that into the code, and it does what I need it to do.


    Thank you for your help with this so far - I am going to give the document a full test and make sure there is nothing else that comes up.


    Thanks again,


    Regards


    Thursday, September 10, 2009 12:37 AM