We help IT Professionals succeed at work.

Copy multiple pages in Word using VBA

gisvpn
gisvpn asked
on
6,953 Views
1 Endorsement
Last Modified: 2009-12-16
Hello -

I would like to know if it is possible to copy pages in MS Word using VBA from one document to another. I would just like to be able to copy say for 3 and 56 from one document to another - is this possible ?

Thanks

GISVPN
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
This will copy to a new document(as in your previous quetion)

Private Sub CommandButton2_Click()
    Dim docNew As Word.Document
    Dim Path As String
    Dim rng As Range
    Dim p1 As Integer
    Dim p2 As Integer
   
    p1 = 3
    p2 = 56
   
    Set rng = ActiveDocument.Range
    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1
    rng.Start = Selection.Start
    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1 + 2
    rng.End = Selection.Start
    rng.Copy
    Set docNew = Application.Documents.Add
    docNew.Bookmarks("\EndOfDoc").Range.Paste
   
    Path = Application.Options.DefaultFilePath(wdDocumentsPath)
    docNew.SaveAs Path & "\" & TextBox4.Text
    docNew.Close
End Sub



Author

Commented:
Hi Graham - thats great thanks for this.

Can i ask a few things to ensure that i understand how it works..


What does this do (I think i get the general idea)

Set rng = ActiveDocument.Range
    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1
    rng.Start = Selection.Start
    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1 + 2
    rng.End = Selection.Start
    rng.Copy

what does this do ?

docNew.Bookmarks("\EndOfDoc").Range.Paste


GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
Set rng = ActiveDocument.Range 'instantiate (create) a Range object. There is no 'New' method for a Range, so we just copy an existing object.

    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1 ' I would like to use another range object here, but some GoTo methods only work on the Selection. This is one of them'
    rng.Start = Selection.Start 'Make the range start at the beginning of the page just found
    Selection.GoTo wdGoToPage, wdGoToAbsolute, p1 + 2 ' This is an error. It should read p2+1, to find the beginning of the page following p2. I was working with adjacent pages (3 and 4), so the result was the same.
    rng.End = Selection.Start ' now definr the end of the range
    rng.Copy 'copy to the clipboard in the usual way.

what does this do ?

docNew.Bookmarks("\EndOfDoc").Range.Paste ' we have to know where to paste things. I guessed that the end of the document would be OK here. Using this hidden, built-in bookmark is one way of finding the end of a document.

Author

Commented:
Hey there,

I am getting this error on this line

Set rng = ActiveDocument.Range

you cannot use this method as no text is seleted.


any ideas :)

Author

Commented:
my fault  - please ignore the above - i had other code which was out of place in there which meant it was causing this not to work :)

Author

Commented:
I am having problems with adding another page :)

I would like to copy pages 2 and 3 and then 45 to 45...

how can i do this with teh above code as the Activedocument changes ?
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
I'll try this thanks !

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.