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

Unable to Select FirstTblInDoc In New Document

Contributor ,
Dec 11, 2022 Dec 11, 2022

Copy link to clipboard

Copied

I created a new document and a new masterpage from it, then inserted a table using the Table > Insert tool.  I can manually select the entire table by clicking on the table anchor, but am unable to select the table with my script...

doc = app.ActiveDoc;
tbl = doc.FirstTblInDoc;
var foundtext = tbl.MakeTblSelection (Constants.FF_SELECT_WHOLE_TABLE, 0, 0, 0);

The script works fine with earlier tables I built using a page from an old document that already had a table in it, but it doesn't work with this new document. Without being able to select the table using ExtendScript, I'm unable to read the contents of the cells into my script.  Any ideas on what I'm missing here? 

Views

181

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 2 Correct answers

Community Expert , Dec 11, 2022 Dec 11, 2022

The first table in the document need not be the first/only one on the body pages. With the standard FM templates there are tables on the reference pages...

So you need to loop through the tables until you get one on the body pages.

Votes

Translate

Translate
Community Expert , Dec 12, 2022 Dec 12, 2022
var doc, textList, count, i, tbl;

doc = app.ActiveDoc;

// Get all of the tables in the document's main flow.
// Text items are always in document order.
textList = doc.MainFlowInDoc.GetText (Constants.FTI_TblAnchor);
// If you want to loop through all of the tables, you can do this:
count = textList.length;
for (i = 0; i < count; i += 1) {
    tbl = textList[i].obj // The table object.
    // Do something with this table.
    
}

// If you only care about the first table, do this:
if (textList
...

Votes

Translate

Translate
Community Expert ,
Dec 11, 2022 Dec 11, 2022

Copy link to clipboard

Copied

The first table in the document need not be the first/only one on the body pages. With the standard FM templates there are tables on the reference pages...

So you need to loop through the tables until you get one on the body pages.

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 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

var doc, textList, count, i, tbl;

doc = app.ActiveDoc;

// Get all of the tables in the document's main flow.
// Text items are always in document order.
textList = doc.MainFlowInDoc.GetText (Constants.FTI_TblAnchor);
// If you want to loop through all of the tables, you can do this:
count = textList.length;
for (i = 0; i < count; i += 1) {
    tbl = textList[i].obj // The table object.
    // Do something with this table.
    
}

// If you only care about the first table, do this:
if (textList.length >  0) {
    tbl = textList[0].obj // First table in the main flow.
    // Do something with this table.
}

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
Contributor ,
Dec 12, 2022 Dec 12, 2022

Copy link to clipboard

Copied

LATEST

My thanks to both of you for your replies.  Since each table is a separate document, I'm not using reference pages, but I do run into that situation on some of the documents we edit, so it's a very useful tip.  Rick...your code solved my immediate problem.  I've modified my script and can now move all of my tables to new blank documents rather than the cut and paste fashion I was using.  The problem with cutting and pasting was that the resulting documents retained the original document Master Pages, which were causing a number of headaches in trying to modify them.  I marked both answers correct.   

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