2 Replies Latest reply on Aug 22, 2008 11:09 AM by (Keith_Cooper)

    VB code for fitting graphics, deleting pages, updating TOC

      Thanks again to all who helped me to find the object model info and other specifics I needed for completing my VB project. I'm not sure if this will be of any use to those who helped me, but at least I can "pay it forward".

      The following code snippets are from my live project, with a bunch of ancillary code stripped away. I think I included everything needed for these three bits of functionality (fit graphics proportionally to frames, delete pages, update TOC), but if I accidentally left out something important, let me know (respond to this thread, as I'm not able to monitor all the threads). Note that in some cases, the code expects values in variables to be set outside of this code. I'll trust that anyone using this code can do that, and I've noted where this takes place.

      My document uses only single pages, not facing-page spreads, so code referencing spreads works for me--but if you're using actual spreads, you may need to change the code to reference pages instead of spreads. Please bear in mind, I'm pretty new to ID, so if I misunderstood how pages and spreads (or anything else) work, please be kind. It's working for me, and hopefully will for others.

      Be advised that deleting spreads (and possibly even fitting graphics) with LOCKED content (in the case of deleting, if ANYTHING on the page is locked) will cause InDesign to CRASH. If you have locked content on your pages, unlock it first either manually or through code (I have code for that as well, if anyone needs it).

      The code for deleting spreads allows you to specify the spreads to delete by LABEL. I haven't figured out (anyone?) how to set labels through ID itself, but I was able to do it in code.

      Lastly, please be advised that the forum may not properly indent or format the code. If it doesn't look pretty, it's not my doing. :)

      VB Code follows:

      Dim InDesign As Object
      Dim InDesignDocument As Object
      Dim LinkCt As Integer
      Dim SpreadsCt As Integer
      Dim PageLabel As String
      Dim TOCStylesCt As Integer
      Dim i As Integer
      Dim p As Integer


      'Create an InDesign object reference and open the InDesign
      'document specified by DocumentFilespec. Note that if InDesign
      'is and the selected document are already open, the in-memory
      'copies will be used.
      Set InDesign = CreateObject("Indesign.Application.CS3")
      Set InDesignDocument = InDesign.Open(DocumentFilespec)


      'Loop through all linked graphic frames and fill
      'frame proportionally.
      LinkCt = CInt(InDesignDocument.Links.Count)
      For i = 1 To LinkCt
      InDesignDocument.Links(i).Parent.Fit 1718185072
      Next i


      'This code deletes selected spreads (or pages if not using facing
      'pages). It expects each spread to have a label, and it expects
      'an array DeletePages to contain a list of the labels for pages
      'to be deleted. DeletePagesCt is a count of the total number of
      'items in the DeletePages array
      For i = 1 To DeletePagesCt
      SpreadsCt = InDesignDocument.Spreads.Count
      For p = 1 To SpreadsCt
      PageLabel = Trim(InDesignDocument.Spreads(p).Label)
      If PageLabel <> "" And DeletePages(i) = PageLabel Then
      InDesignDocument.Spreads(p).Delete
      Exit For
      End If
      Next p
      Next i



      'This code updates the Table of Contents for a given TOC Style Name
      '(ExpectedTOCStyleName).
      TOCStylesCt = CInt(InDesignDocument.TOCStyles.Count)
      For i = 1 To TOCStylesCt
      If InDesignDocument.TOCStyles.Item(i) = ExpectedTOCStyleName Then
      InDesignDocument.CreateTOC InDesignDocument.TOCStyles.Item(i), True
      Exit For
      End If
      Next i