Link to home
Start Free TrialLog in
Avatar of gisvpn
gisvpnFlag for United States of America

asked on

Copy multiple pages in Word using VBA

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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



Avatar of gisvpn

ASKER

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


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.
Avatar of gisvpn

ASKER

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 :)
Avatar of gisvpn

ASKER

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 :)
Avatar of gisvpn

ASKER

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 ?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gisvpn

ASKER

I'll try this thanks !