[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Copy multiple pages in Word using VBA

Posted on 2007-10-04
8
Medium Priority
?
5,657 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
1
Comment
Question by:gisvpn
  • 5
  • 3
8 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20013130
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



0
 

Author Comment

by:gisvpn
ID: 20013149
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


0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20013284
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:gisvpn
ID: 20014356
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 :)
0
 

Author Comment

by:gisvpn
ID: 20014366
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 :)
0
 

Author Comment

by:gisvpn
ID: 20014422
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 ?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 20014498
Just do it twice


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 = 2
    p2 = 3
   
    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
   
    p1 = 45
    p2 = 45
    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

What do you men by the 'Activedocument changes'
0
 

Author Comment

by:gisvpn
ID: 20047260
I'll try this thanks !
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Progress
Loops Section Overview

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question