6 Replies Latest reply on May 7, 2009 1:39 AM by chrisnaylor

    VBA script for automatically indexing topics

    chrisnaylor

      A big disadvantage with InDesign's indexing system is that you cannot automatically index all occurences of topics currently in the index.

       

      Instead, you have to find an occurence of each topic in the document itself and go to the Index menus and then click New Page Reference...Add All, for each index topic individually.

       

      To get around this, I have been trying to produce a VBA script to automatically index all occurrences in a document of the topics currently in the document index.

       

      I have been using the MS Excel VBA development environment but find the definitions of the object model in the Excel VBA browser rather inadequate and ambiguous .

       

      Is there a more thorough reference work for these?

       

      The plan was to start by taking each individual topic in the index which I tried to access with something along the lines of:

       

      MyDocument.Index.Topic(1)

       

      This hasn't worked.

       

      The plan then was to take each index topic and use it to find an occurence in the document of that topic and then use that occurence to invoke the New Page Reference...Add All option to index all occcurences of that selected term, using something like:

       

      MyDocument.PageReferences.Add

       

      Again, without success.

       

      Any guidance or suggestions would be appreciated.

        • 1. Re: VBA script for automatically indexing topics
          Ildhund Level 3

          Not sure if this will help, but have a look at

          http://marcautret.free.fr/geek/indd/indexbrut/en.php

           

          It might save you from reinventing the wheel.

          Noel

          • 2. Re: VBA script for automatically indexing topics
            Peter Kahrel Adobe Community Professional & MVP

            You'd first need a reference to the index and its topics. In javaScript (pretty similar to VB) you'd do it like this

             

            myIndex = app.activeDocument.indexes[0];
            myTopics = myIndex.allTopics;

             

            Then cycle through the topics:
            for (var i = 0; i < myTopics.length; i++)
               {

               // find all occurrences of a topic

               var myFound = myDoc.findText ()

               // Now cycle through the found text occurrences and create a page reference for each item

               // You have to work from end to beginning as adding page references adds "character" to the text

               for (var j = myFound.length-1; j > -1; j--)

                  myTopics[i].pageReferences.add (myFound[j], PageReferenceType.currentPage);

               }

             

            It shouldn't be too difficult to translate this into VB.

             

            Peter

            1 person found this helpful
            • 3. Re: VBA script for automatically indexing topics
              chrisnaylor Level 1

              Dear Peter,

               

              Many thanks for your posting, that was very helpful.

               

              This is the state of the script so far (I have added a large number of MsgBox entries - these aren't needed, they are just for debugging purposes during development):

              :

              'VBA script which tries to work through all of the topics in the index and automatically index each occurence of them in the current text frame:

              main
              Function main()

              Set MyInDesign = CreateObject("InDesign.Application.CS4")

              If MyInDesign.Documents.Count > 0 Then

                  Set MyDocument = MyInDesign.ActiveDocument
                  Set MyPage = MyDocument.Pages.Item(1)
                  Set MyTextFrame = MyPage.TextFrames.Item(1)
                  Set MyStory = MyTextFrame.ParentStory
                 
                  Set MyIndex = MyDocument.Indexes(1)
                  Set MyTopics = MyIndex.AllTopics
                 
                  MyIndexesCount = MyDocument.Indexes.Count

                  MsgBox "Current indexes Count: " & MyIndexesCount

                  MyIndexTopicsCount = MyTopics.Count

                  MsgBox "Current index topics Count: " & MyIndexTopicsCount
                 
                  For i = 1 To MyTopics.Count Step 1 'work through the topics
              MyIndexTerm=MyTopics(i)

              MsgBox "Current indexes Topic: " & MyIndexTerm

              'search for MyIndexTerm :
                      'Clear Find preferences:
                      MyInDesign.FindTextPreferences = idNothingEnum.idNothing

                      'Set up search paramaters:
                      If MyIndexTerm <> "" Then
                          MyInDesign.FindTextPreferences.FindWhat = MyIndexTerm
                          'Set search options:
                          MyInDesign.FindChangeTextOptions.CaseSensitive = False
                          MyInDesign.FindChangeTextOptions.IncludeFootnotes = False
                          MyInDesign.FindChangeTextOptions.IncludeHiddenLayers = False
                          MyInDesign.FindChangeTextOptions.IncludeLockedLayersForFind = False
                          MyInDesign.FindChangeTextOptions.IncludeLockedStoriesForFind = False
                          MyInDesign.FindChangeTextOptions.IncludeMasterPages = False
                          MyInDesign.FindChangeTextOptions.WholeWord = False
                 
                          'Search for the string:
                          Set MyFoundItems = MyDocument.FindText 'this ought to be limited to the Story, not the Document eg MyStory
                       MsgBox "Found index terms:" & MyFoundItems.Count
                          For j = MyFoundItems.Count To 1 Step -1
                             MyTopics(i).PageReferences.Add MyFoundItems(j)
                          Next
                         'Clear preference:
                          MyInDesign.FindTextPreferences = idNothingEnum.idNothing
                 
                          'Having found it, add page reference:
                       'MyDocument.PageReferences.Add
                      Else
                          MsgBox "No search term specified"
                      End If

              Response=MsgBox ("Continue?", vbYesNo)
              If Response = vbNo then
                Exit For
              End If
                 Next
              'Set up Index options:
              MyIndex.IndexOptions.Title = "Index"
              MyIndex.IndexOptions(1).TitleStyle="Chapter Head"
              MyIndex.IndexOptions(1).ReplaceExistingIndex = True

              'Generate the index:
              MyIndex.Generate
              Else
                  MsgBox ("Please open a document, select an object, and try again.")

              End If
              End Function

               

               

              There are a couple of problem areas still:

               

              1. Index terms appear to be added irrespective of whether or not that particular page reference already exists in the index.

              This can result in duplicate page references under each topic - especially if the script is run several times.

               

              Is there any way of avoiding these duplicate references?

               

              2. I haven't yet suceeded in getting the script to generate the index - so far I've had to do it manually after the script has run.

              The problem lies somewhere in the code to set up the index options and then generate the index.

               

              3. It might be useful to be able to limit the indexing to the curent story rather than the whole document - but I haven't yet got it to do that.

               

              Best wishes.

              • 4. Re: VBA script for automatically indexing topics
                chrisnaylor Level 1

                Dear Noel,

                 

                Many thanks for your posting.

                 

                I had a look at that site but, unless I misunderstood it, I got the impression that the indexing system bypassed InDesign's system of indexing altogether.

                 

                I thought it might be useful to be able to stay within the InDesign indexing system if it was reasonably possible because of the other facilities it might offer.

                 

                Best wishes.

                • 5. Re: VBA script for automatically indexing topics
                  Peter Kahrel Adobe Community Professional & MVP

                  Marc's "index brutal" indeed bypasses InDesign's index and is good for one-off indexes. When you maintain a publication for several editions and plan to add topics and page references for each subsequent edition, then you're better off using InDesign's index.

                   

                  Peter

                  • 6. Re: VBA script for automatically indexing topics
                    chrisnaylor Level 1

                    Dear Peter,

                     

                    Many thanks for your recent posting.

                     

                    Just to return to the general aspect of my original post:

                     

                    Do you know of any reference source which gives details of the InDesign object model?

                     

                    As mentioned, the details given by the MS Excel VB Object Browser are pretty sketchy and getting the coding correct can be a pretty hit or miss matter.

                     

                    Anything that gave fuller details of syntax and usage would be very helpful.

                     

                    Best wishes.

                     

                    Chris