Skip to main content
Fightergator
Inspiring
August 27, 2022
Question

Stepping Through Multiple Open Documents

  • August 27, 2022
  • 1 reply
  • 287 views

I'm trying to step through multiple open documents and do something in each.  Have had no luck searching the forum for an answer...probably something discusssed and forgotten years ago. The procedure I'm using works when the "do something" is:

 

tbl = doc.FirstTblInDoc;
foundText = tbl.MakeTblSelection (Constants.FF_SELECT_WHOLE_TABLE, 0, 0, 0);

 

However, when I try to do something else, like change font color of first paragraph of each document, it just keeps doing it to the first document.  I'm guessing it has something to do with making each document the focus, but I've had no luck figuring out how to do that.  Here's my code...

 

doc = app.FirstOpenDoc;
Alert ("Testing script to step through documents.");
while (doc.ObjectValid()) {

   //Do something in the open document

  doc = doc.NextOpenDocInSession;
}

    This topic has been closed for replies.

    1 reply

    Fightergator
    Inspiring
    August 28, 2022

    P.S. Please disregard.  With a rested mind and some hot coffee I found a discussion between Russ, Klaus G. & K. Daube about setting the focus.  Russ posted, 'that anytime he needed to force a document to the front (that is, "brute force"), he called Open() on it again...something like:  SimpleOpen(oDoc.Name, false);'  If anyone has a better way, please let me know.  My revised script:

     

    doc = app.FirstOpenDoc;
    while (doc.ObjectValid()) {
       var name = doc.Name;
       SimpleOpen(name, false);
       // Do something with the document

    doc = doc.NextOpenDocInSession;
    }

    4everJang
    Legend
    September 1, 2022

    At the beginning of your script, create a document object array in which you collect all applicable documents. Note that there may be visible or invisible documents in the current session which you do not want to mess with. So, at the start of your script, declare an array, then use a loop through all active document in the current session and check their names to see if they should be handled by your script. If so, add the document object to your documents array. Once you have all the Doc objects in the array you can start processing them. This is what I have been doing for a couple of decades and it always works like a breeze.

    Fightergator
    Inspiring
    September 1, 2022

    Thanks for the tip.  I'll give it a try.  Sounds easy enough.  I think I see the logic behind collecting all the applicable documents before I start looping through them.