6 Replies Latest reply on Dec 25, 2016 10:43 PM by Trevorׅ

    How do I merge cells in InDesign CS5 in Visual Basic?

    M F Walker Level 1

      Below is a script which will take each cell in the first column of a table and merge it with the cell on the right. It assumes the cursor is in one of the cells on the right.

       

      I am trying to upgrade this so that if the cursor is placed in any cell in the table (except in the last column), then the script is run, it will merge each cell in the column of the cursor with the cell on the right. I assume there are no prior merges in the table.

       

      Set myInDesign = CreateObject("InDesign.Application")

      If myInDesign.Documents.Count <> 0 Then

        Set myDocument = myInDesign.Documents.Item(1)

        If myInDesign.Selection.Count <> 0 Then

        myResponse = MsgBox("Is an insertion point in column one of the table?", vbYesNo)

        If myResponse = 6 Then

            If TypeName(myInDesign.Selection.Item(1).Parent) = "Cell" Then

              Set myCell = myInDesign.Selection.Item(1).Parent

              Set myColumn = myCell.ParentColumn

        Set myTable  = myInDesign.Selection.Item(1).Parent.Parent

              ' Set myColumNumber = MyTable.myColumn.Item

              For myCounter = 1 to myColumn.Cells.Count

                 myTable.Rows.Item(myCounter).Cells.Item(1).Merge myTable.Rows.Item(myCounter).Cells.Item(2)

              Next

            MsgBox ("Done")

            End If

        Else MsgBox ("Place insertion point correctly and restart script")

        End If

      End If

      End If

        • 1. Re: How do I merge cells in InDesign CS5 in Visual Basic?
          Trevorׅ Adobe Community Professional

          Hi M F

          This is from the SDK

           

            Set myDocument = myInDesign.Documents.Item(1)

           

            Set myStory = myDocument.Stories.Item(1)

            Set myTable = myStory.Tables.Item(1)

            Rem Merge all of the cells in the first column.

            myTable.Cells.Item(1).Merge myTable.Columns.Item(1).Cells.Item(-1)

            Rem Convert column 2 into 2 cells (rather than 4).

            myTable.Columns.Item(2).Cells.Item(-1).Merge myTable.Columns.Item(2).Cells.Item(-2)

            myTable.Columns.Item(2).Cells.Item(1).Merge myTable.Columns.Item(2).Cells.Item(2)

            Rem Merge the last two cells in row 1.

            myTable.Rows.Item(1).Cells.Item(-1).Merge myTable.Rows.Item(1).Cells.Item(-1)

            Rem Merge the last two cells in row 3.

            myTable.Rows.Item(3).Cells.Item(-2).Merge myTable.Rows.Item(3).Cells.Item(-1)

           

           

          HTH

           

          Trevor

          • 2. Re: How do I merge cells in InDesign CS5 in Visual Basic?
            M F Walker Level 1

            Trevor, I have already seen this, and used part of it in the script I submitted. I am still trying to find how to identify the column in which the cursor is displayed.

            • 3. Re: How do I merge cells in InDesign CS5 in Visual Basic?
              Trevorׅ Adobe Community Professional

              Hello Again

               

              This will Do what you want. Sorry about my above stupidity.

               

               

              ' Absolutely Zero Error Checking
              ' Will Hopefully merge selected cells
              ' This will only Work if text or insertion point of a cell isselected :-;
              
              Set myInDesign = CreateObject("InDesign.Application.CC.2015")
              Set mySelection = myInDesign.Selection.Item(1)
              Set MyCell = mySelection.Parent
              Set MyCellsRow = MyCell.ParentRow
              Set MyCellsColumn = MyCell.ParentColumn
              Set MyCellsRowsLastCell = MyCell.ParentRow.Cells.Item(-1)
              If MyCellsRowsLastCell <> MyCell Then
                  MyCellsColumn = CInt(MyCellsColumn) ' Change to Int for Index reference
                  MyCell.Merge MyCellsRow.Cells.Item(MyCellsColumn + 1)
              End If
              

               

               

              To Merge selected cell do

              ' Absolutely Zero Error Checking
              ' Will Hopefully merge selected cells
              ' This will only Work if cells are selected :-;
              
              Set myInDesign = CreateObject("InDesign.Application.CC.2015")
              Set mySelection = myInDesign.Selection.Item(1)
              Set MyCells = mySelection.Cells
              Set firstCell = MyCells.Item(1)
              Set lastCell = MyCells.Item(-1)
              firstCell.Merge lastCell
              
              ' In JavaScript which is a much more civilized language one could do
              ' app.selection[0].cells[0].merge(app.selection[0].cells[-1]);
              ' Which is the vbs equivalent of
              ' myInDesign.Selection.Item(1).Cells.Item(1).Merge myInDesign.Selection.Item(1).Cells.Item(-1)
              

               

              HTH

               

              Trevor

               

              P.s.

              You should note that it's not a good idea to script InDesign with Visual Basic (Script) unless you have good reason to.

              JavaScript is much more recommended

              • 4. Re: How do I merge cells in InDesign CS5 in Visual Basic?
                M F Walker Level 1

                Thank you Trevor. I now have a working script.

                • 5. Re: How do I merge cells in InDesign CS5 in Visual Basic?
                  Laubender Adobe Community Professional & MVP

                  Hi,
                  if answer 3 is the correct answer, give that one the "Correct Answer" label and not your comment on the correct answer.

                  If answer 3 is not the correct one and you figured out yourself what the correct code is, please post that code and make it the "Correct Answer".

                   

                  Thanks,
                  Uwe

                  • 6. Re: How do I merge cells in InDesign CS5 in Visual Basic?
                    Trevorׅ Adobe Community Professional

                    Hello M F Walker

                     

                    4 is a correct answer.

                    It is correct to say thank you.

                     

                    3 is the correct answer as it answers the question

                    Both scripts work.

                    The 1st one will merge the cell with selected text with the cell to the right of it.

                    The 2nd one merges all selected cells.

                    Please mark 3 as correct.

                     

                    Ta very much.

                     

                    Trevor