• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Explorer ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

807

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Dec 23, 2016 Dec 23, 2016

Thank you Trevor. I now have a working script.

Votes

Translate

Translate
Guru ,
Dec 19, 2016 Dec 19, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 23, 2016 Dec 23, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 23, 2016 Dec 23, 2016

Copy link to clipboard

Copied

Thank you Trevor. I now have a working script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 24, 2016 Dec 24, 2016

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Dec 25, 2016 Dec 25, 2016

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines