6 Replies Latest reply on Jan 10, 2011 6:10 AM by Kasyan Servetsky

    Duplicating Pages to Another Document

    Ken Krugh

      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

        • 1. Re: Duplicating Pages to Another Document
          Kasyan Servetsky Level 5

          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[i].duplicate(LocationOptions.AFTER, lastPage);
          }
          

           

          Kasyan

          • 2. Re: Duplicating Pages to Another Document
            Kasyan Servetsky Level 5

            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

            • 3. Re: Duplicating Pages to Another Document
              Ken Krugh Level 1

              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

              • 4. Re: Duplicating Pages to Another Document
                Kasyan Servetsky Level 5

                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.

                js.png

                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:

                vb.png

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

                 

                Kasyan

                • 5. Re: Duplicating Pages to Another Document
                  Peter Kahrel Adobe Community Professional & MVP

                  Hi Kasyan,

                   

                  itemByRange() returns not an array but a collection. Try this:

                   

                  pp = app.documents[0].pages.itemByRange (0,4);

                   

                  pp.constructor.name; // => Page

                  pp.length; // => error

                   

                  pp.getElements().constructor.name; // => Array

                  pp.getElements().length; // => 5

                   

                  Peter

                  • 6. Re: Duplicating Pages to Another Document
                    Kasyan Servetsky Level 5

                    Thank you for clarifying this, Peter.

                     

                    Kasyan