5 Replies Latest reply on Dec 17, 2010 7:02 AM by FrankB62

    Problems with accessing all text in a document

    FrankB62 Level 1

      I am having huge problems with accessing all text within a document using VBScript. I am fairly new to InDesign, and it is quite possible that I am doing things the wrong way, so any advice is much welcome.


      I have a document originally created in FrameMaker, and converted to InDesign via some 3rd party software (I don't know which). The document has then been handled manually in InDesign afterwards, and pages have been added, edited and deleted.


      Now, my task is to traverse through all tekst in the document, and do some modifications via a script, but I can't get it to treat all text in the document. To try and isolate the problem, I made some scripts that simply deletes all text in the document (the document has 228 pages in total).


      Here are some snippets of code:


      A (this code clears all text from page 1 to 47, the rest is left untouched)

      For Each Story In Document.Stories

           Story.Contents = ""



      B (this code clears all text from page 1 to 51)

      For Each TextFrame In Document.TextFrames

           TextFrame.Contents = ""



      C (this code clears all text from page 1 to 78)

      For Each Spread In Document.Spreads

           For Each TextFrame In Spread.TextFrames

                TextFrame.Contents = ""




      I don't know of other ways than theese to access all text, and apparently neither of them work. I am especially puzzled about the difference between case B and C, because how can a spread within a document contain text frames which the document itself does not contain?


      Does anyone know of a method to traverse through a document, and be 100% sure that all text is included?

        • 1. Re: Problems with accessing all text in a document
          [Jongware] Most Valuable Participant

          Can you test with Export this file to IDML, open the exported one, and then try any or all of your scripts? (Be careful not to overwrite your original file. IDML is reported to have a "100% safe round-trip" -- per the latest update. I think they said the same before that update.)


          The reason is that your code looks good and ought to have worked just fine; and you thoughtfully mentioned the file wasn't originally created inside InDesign. At times, pieces of a document go a-float and ID cannot reliably tack them together; exporting, then importing again ought to free all debris.


          (I just thought of this: a quicker method could be to "Save As" your file. It clears out most of the file, where exporting/importing does a more thorough job but takes longer. So you could just this one a first try.)

          • 2. Re: Problems with accessing all text in a document
            FrankB62 Level 1

            I have tried exporting as IDML, but it makes no difference.


            I then tried constructing my loop using item index instead, and it gives an interresting result:


            For StoryNo = 1 To Document.Stories.Count
                 Set Story = Document.Stories.Item(StoryNo)

                 Story.Contents = ""



            Document.Stories.Count is 1434 when I start this loop, but when the index reaches 30, it is changed to 1076, and stays at that value through the rest of the loop (and the script gives an error, of course, when the index goes out of bounds).


            If I loop backwards instead (Step -1), then it works, and all text in the document is deleted.


            My conclusion is that there is something severely wrong with the document, and the various collections are not set properly.

            • 3. Re: Problems with accessing all text in a document
              [Jongware] Most Valuable Participant

              If counting backwards works, then use that. Some object manipulations may change the internal state of ID's object counters, and you'll get unreliable results trying to work from start to end.


              An afterthought: did you have anchored objects in your original text? Text inside these also count as a separate story, but it's removed when you empty out its parent story -- thus, possibly, throwing off the original counts.

              • 4. Re: Problems with accessing all text in a document
                FrankB62 Level 1

                There are anchored objects in the document (more than 3000), but none of those are removed by my script. The story count only changes once (from 1434 to 1076) when I clear one specific story, so to me it look like there is something wrong internally with that collection, and InDesign repairs it on the way.


                I am going to make some tests with looping backwards, but I am certainly not comfortable with the whole situation. Who knows what else could be wrong with that document.

                • 5. Re: Problems with accessing all text in a document
                  FrankB62 Level 1

                  Also note: My example with clearing all text within each story is only a debug method to find out where things go wrong. The real scenario is to change the text within specific pharagraphs, but that makes no difference to how the looping through stories work. With my For Each loop through the stories (case A), it only changes text in paragraphs on the first 47 pages, same as my example script only deletes text on the first 47 pages.