Skip to main content
January 7, 2011
Answered

Duplicating Pages to Another Document

  • January 7, 2011
  • 2 replies
  • 800 views

Happy New Year!

Working in CS3, VBA, PC.

I need to merge several InDesign documents in an on going project and have a script written that works by duplicating each individual page from one document to another. My code to duplicate each page looks like this.

Call FromDoc.Pages(pgs%).Duplicate(idLocationOptions.idAfter, ToDoc.Pages(-1))

I THOUGHT one the following would work to duplicate them all at once like you can with the Pages panel but I'm getting an error that the object doesn't support that method.

Call FromDoc.Pages.ItemByRange(FromDoc.Pages(1), FromDoc.Pages(-1)).Duplicate(idLocationOptions.idAfter, ToDoc.Pages(-1))
Call FromDoc.Pages.ItemByRange(1, -1).Duplicate(idLocationOptions.idAfter, ToDoc.Pages(-1))

If this simply not possible or am I missing something.

Many thanks,

Ken

This topic has been closed for replies.
Correct answer Kasyan Servetsky

Here is a VB version:

Dim myInDesign As InDesign.Application
Dim FromDoc As InDesign.Document
Dim ToDoc As InDesign.Document
Dim ThisPage As InDesign.Page
Set myInDesign = CreateObject("InDesign.Application.CS3")
Set FromDoc = myInDesign.Documents.Item("FromDoc.indd")
Set ToDoc = myInDesign.Documents.Item("ToDoc.indd")
Set Pages = FromDoc.Pages
For Each ThisPage In Pages
    Set LastPage = ToDoc.Pages.LastItem
    ThisPage.Duplicate idLocationOptions.idAfter, ToDoc.Pages(-1)
Next

Kasyan

2 replies

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
January 7, 2011

Here is a VB version:

Dim myInDesign As InDesign.Application
Dim FromDoc As InDesign.Document
Dim ToDoc As InDesign.Document
Dim ThisPage As InDesign.Page
Set myInDesign = CreateObject("InDesign.Application.CS3")
Set FromDoc = myInDesign.Documents.Item("FromDoc.indd")
Set ToDoc = myInDesign.Documents.Item("ToDoc.indd")
Set Pages = FromDoc.Pages
For Each ThisPage In Pages
    Set LastPage = ToDoc.Pages.LastItem
    ThisPage.Duplicate idLocationOptions.idAfter, ToDoc.Pages(-1)
Next

Kasyan

January 9, 2011

Thanks Kasyan, that's pretty much what I ended up with. I was hoping to be able to move all the pages at once instead of iterating through them but that is at least working now.

If I find anything out regarding what I was doing wrong trying to use ItemByRange I'll post back.

Thanks again,

Ken

Kasyan Servetsky
Legend
January 9, 2011

In JS this works:

var fromDoc = app.documents.item( 'FromDoc.indd' ); 
var toDoc = app.documents.item( 'ToDoc.indd' );
var pages = fromDoc.pages.itemByRange( fromDoc.pages.firstItem(), fromDoc.pages.lastItem() );
var myType = pages.constructor.name;
pages.duplicate( LocationOptions.AFTER, toDoc.pages.lastItem() );

The same code translated to VB doesn't:

Private Sub Command1_Click()
    Dim myInDesign As InDesign.Application
    Dim FromDoc As InDesign.Document
    Dim ToDoc As InDesign.Document
    Dim ThisPage As InDesign.Page
    Set myInDesign = CreateObject("InDesign.Application.CS3")
    Set FromDoc = myInDesign.Documents.Item("FromDoc.indd")
    Set ToDoc = myInDesign.Documents.Item("ToDoc.indd")
    Set Pages = FromDoc.Pages.ItemByRange(FromDoc.Pages.FirstItem, FromDoc.Pages.LastItem)
    myType = TypeName(Pages)
    Pages.Duplicate dLocationOptions.idAfter, ToDoc.Pages.LastItem
End Sub

I wonder why?

In JS variable myType returns "Page" but it's a somewhat strange page to me: each property is an array of properties.

Both references -- JS&VB -- state that pages.itemByRange return array of pages, but JS returns Page for some reason.

pages.constructor.name == "Page"

Result: true

pages.constructor.name == "Array"
Result: false

And, as we can see in the reference, Page has duplicate method.

However, VB code returns array of page objects:

I think that's why it works in JS but not in VB.

Kasyan

Kasyan Servetsky
Legend
January 7, 2011

This works in JS, but duplicates pages backwards: from last to first.

var fromDoc = app.documents.item('FromDoc.indd'); 
var toDoc = app.documents.item('ToDoc.indd');
var pagesToMove = fromDoc.pages.itemByRange(0, -1);
pagesToMove.duplicate(LocationOptions.AFTER, toDoc.pages.item(-1));

So I think you need to do something like this:

var lastPage;
var fromDoc = app.documents.item('FromDoc.indd');
var toDoc = app.documents.item('ToDoc.indd');
var pagesToMove = fromDoc.pages;

for (var i = 0; i < pagesToMove.length; i++) {
    lastPage = toDoc.pages[toDoc.pages.length-1];
    pagesToMove.duplicate(LocationOptions.AFTER, lastPage);
}

Kasyan