2 Replies Latest reply on Oct 15, 2013 1:28 PM by mrslother

    Loop on all items in a collection


      I need to loop on all items in a collection in a VB.NET program that is using the InDesign API. For example, I want to loop on all master spreads in a document. Here is my sample code:


          Dim I As Integer = 0

          Dim MyApp As InDesign.Application = Nothing

          Dim MyDoc As InDesign.Document = Nothing

          Dim MyPage As InDesign.Page = Nothing


          MyApp = CType(Activator.CreateInstance(Type.GetTypeFromProgID("InDesign.Application"), True), InDesign.Application)


          MyDoc = CType(MyApp.Documents.Add(False), InDesign.Document)


          With MyDoc

            With .DocumentPreferences

              .PageHeight = "8.5i"

              .PageWidth = "5.5i"

            End With


            With .ViewPreferences

              .HorizontalMeasurementUnits = InDesign.idMeasurementUnits.idPoints

              .VerticalMeasurementUnits = InDesign.idMeasurementUnits.idPoints

            End With


            If .MasterSpreads.Count > 0 Then

              For I = 0 To .MasterSpreads.Count - 1

                Dim ThisMasterSpread As InDesign.MasterSpread = CType(.MasterSpreads.Item(I), InDesign.MasterSpread)

              Next I

            End If

          End With


      When it executes, I get an error on the last Dim statement that defines ThisMasterSpread. It highlights .MasterSpreads.Item(I) and gives the following error:


      COMException was unhandled: The requested member of the collection does not exist.


      How do I successfully loop on all master spreads?