PDA

View Full Version : Solved: How to copy current page to existing document?



Simon Lloyd
11-07-2007, 04:54 AM
Hi all, i have been trying to automate copying the currently viewed page from one document to an existing document but to no avail, can anyone help with this?

Here's what i have been trying so far:

ActiveDocument.Bookmarks("\page").Range.Select
Selection.Copy Destination:="G:\Work Conversions\Rovema Bagger Training Manual Editing test.doc"

TonyJollans
11-07-2007, 05:48 AM
Copy in Word does not have a Destination option (that's an Excel thing). You must Copy and Paste as separate operations.

Simon Lloyd
11-07-2007, 07:21 AM
Tony, thanks for the prompt reply, i don't know a great deal about word VBA language (nothing in fact!) but i have come up with this but it fails as it cant find the file which is clearly at that address!

Sub shtcopy()
ActiveDocument.Bookmarks("\page").Range.Select
Selection.Copy
Application.Documents.Open ("G:\Work Conversions\Rovema Bagger Training Manual Editing test")
ActiveDocument.Paste
End Sub

Simon Lloyd
11-07-2007, 07:24 AM
Ok, tried a test document and it opened it, it may be that the doc i am trying to open is in RTF, however when it comes to the ActiveDocument.Pasteit fails giving error 438 Object doesnt support...
Any ideas?

TonyJollans
11-07-2007, 08:26 AM
Try ..

Activedocument.Range.paste

Simon Lloyd
11-07-2007, 09:29 AM
Again Tony thanks for the reply, that works, only trouble is it overwrites all the current pages, how do i use the "InsertAfter" property?

Sub shtcopy()
ActiveDocument.Bookmarks("\page").Range.Select
Selection.Copy
Application.Documents.Open ("G:\Work Conversions\Rovema Bagger Training Manual")
ActiveDocument.Range.Insert(?????)
End Sub

TonyJollans
11-07-2007, 11:01 AM
InsertAfter, and similar, all work with text, without formatting, whilst copy and paste copy formatting as well.

If you want the copied page at the beginning of the other document. See if this works for you ..

Set NewDoc = Application.Documents.Open ("G:\Work Conversions\Rovema Bagger Training Manual")
NewDoc.Range(0, 0).FormattedText = ActiveDocument.Bookmarks("\page").Range.FormattedText

TonyJollans
11-07-2007, 11:03 AM
Oops, it won't quite work like that!
Try this ..

Set CurrentDoc = ActiveDocument
Set NewDoc = Application.Documents.Open ("G:\Work Conversions\Rovema Bagger Training Manual")
NewDoc.Range(0, 0).FormattedText = CurrentDoc.Bookmarks("\page").Range.FormattedText

(edited because I messed up again)

Simon Lloyd
11-08-2007, 12:25 PM
Tony, thanks for sticking with this!, ideally i want to specify which page the copied page should come after, however the code fails with the error message "Error 5419, The requested member of the collection doess not exist!", on the line indicated:

Sub shtcopy()
Set CurrentDoc = ActiveDocument
Set NewDoc = Application.Documents.Open("G:\Work Conversions\Rovema Bagger Training Manual converted")
NewDoc.Range(0, 0).FormattedText = CurrentDoc.Bookmarks("\page").Range.FormattedText''''fails on this line
ActiveDocument.Bookmarks("\page").Range.Select
End Sub

TonyJollans
11-08-2007, 01:09 PM
Well, well! That's something I've learnt today. Sorry for misleading you.

You can't use the predefined "\Page" bookmark on anything other than the Active Document. Try this instead ...

Sub shtcopy()
Set CurrentPage = ActiveDocument.Bookmarks("\Page").Range
Set NewDoc = Application.Documents.Open("G:\Work Conversions\Rovema Bagger Training Manual converted")
NewDoc.Range(0, 0).FormattedText = CurrentPage.FormattedText
ActiveDocument.Bookmarks("\page").Range.Select
End Sub

TonyJollans
11-08-2007, 01:20 PM
If you want to specify a page in th etarget document, look at the GoTo method

Simon Lloyd
11-08-2007, 02:06 PM
Tony, thanks that worked, it pasted the page as the first page. With regards to the GoTo method does Word have an InputBox like excel to input the page i want to paste after?, i am integrating 6 manuals into one so manipulating around 300 pages, i was just trying to save a lot of manual work, i am fairly confident with Excel VBA but have never worked with VBA in Word.

Simon Lloyd
11-08-2007, 02:16 PM
This is what i tried (naturally it doesn't work!) but i get error message "Runtime Error 4120, Bad Parameter"

Sub shtcopy()
Set CurrentPage = ActiveDocument.Bookmarks("\Page").Range
Set NewDoc = Application.Documents.Open("G:\Work Conversions\Rovema Bagger Training Manual converted")
ActiveDocument.GoTo What:=NewDoc, which:=wdGoToFirst, Count:=2
NewDoc.Range(0, 0).FormattedText = CurrentPage.FormattedText
ActiveDocument.Bookmarks("\page").Range.Select
End Sub

Simon Lloyd
11-08-2007, 02:55 PM
I have been able to use an inputbox but it doesn't change anything it still pastes at the first page!

Sub shtcopy()
Set CurrentPage = ActiveDocument.Bookmarks("\Page").Range
CurrentPage.Copy
Set NewDoc = Application.Documents.Open("G:\Work Conversions\Rovema Bagger Training Manual converted")
IB = InputBox("Enter Page Number To Paste After", "Page Picker")
NewDoc.GoTo What:=wdGoToPage, which:=wdGoToFirst, Count:=IB
Selection.Paste
ActiveDocument.Bookmarks("\page").Range.Select
End Sub

TonyJollans
11-08-2007, 11:24 PM
Hi Simon,

Occasionally I come across situations where Copy and Paste cannot be used and I have been considering possible alternatives - one reason why I used the FormattedText earlier. As you have reverted to Copy / Paste, let's stick with that.

Now GoTo. You need to be consistent in your use of object. You GoTo somewhere in the Document and then you Paste somewhere in the Selection; the one is not the same as the other. GoTo (page) will give you a Range at the start of the page, where you can paste - directly if you wish, something like ..


NewDoc.GoTo(wdGoToPage, wdGoToFirst, IB + 1).Paste


(note the IB + 1 as you have prompted for the page after which to paste, and GoTo goesto the start of a page)

Finally, just out of interest, why are you selecting the page at the end?

Simon Lloyd
11-09-2007, 01:06 PM
Tony, thanks again!, i didnt know i was trying to select a page at the end!, the code below is what i have and seems to work a treat, as i said i have very little knowledge of Word VBA so please forgive me my failings.


Sub shtcopy()
Set CurrentPage = ActiveDocument.Bookmarks("\Page").Range
CurrentPage.Copy
Set NewDoc = Application.Documents.Open("G:\Work Conversions\Rovema Bagger Training Manual converted")
IB = InputBox("Enter Page Number To Paste After", "Page Picker")
NewDoc.GoTo(wdGoToPage, wdGoToFirst, IB + 1).Paste
End Sub

TonyJollans
11-09-2007, 01:43 PM
Glad it's now working :)