Skip to main content
Fightergator
Inspiring
December 11, 2022
Answered

Unable to Select FirstTblInDoc In New Document

  • December 11, 2022
  • 2 replies
  • 420 views

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? 

    This topic has been closed for replies.
    Correct answer frameexpert
    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.
    }
    

    2 replies

    frameexpert
    Community Expert
    frameexpertCommunity ExpertCorrect answer
    Community Expert
    December 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.length >  0) {
        tbl = textList[0].obj // First table in the main flow.
        // Do something with this table.
    }
    
    Fightergator
    Inspiring
    December 12, 2022

    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.   

    K.Daube
    Community Expert
    Community Expert
    December 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.