6 Replies Latest reply on Mar 7, 2008 10:00 AM by (Archie_Tucker)

    Finding File Name And Using It To Place The Graphic Using VBscript

    Level 1
      I'm trying to find a file name (example, <place:E:\logo.tif>) in each text frame if it's there, then remove the text from the frame and then place the file in the frame. This is to get around the feature missing from InDesign that is in PageMaker that allowed you to import inline graphics using tagged text. Below is what I have come up with so far. The file name is currently hard coded in the example below.
      Thanks,
      Archie

      For I = 1 to cInt(myMountNumber)

      Set myTextFrames = myDocument.TextFrames
      Set myCurrentFrame = myTextFrames.Item(I)

      If myCurrentFrame.Texts.ItemByRange (1,7).Contents = "<Place:" Then
      Set myLastCharacter = myCurrentFrame.Characters.Item(-2)
      myCurrentFrame.Texts.ItemByRange (1,myLastCharacter).Item(1).Contents = ""
      myCurrentFrame.InsertionPoints.Item(1).Place ("E:\sxs\pm65\Sundco.jpg")
      myCurrentFrame.Fit idFitOptions.idProportionally
      End If

      Next
        • 1. Re: Finding File Name And Using It To Place The Graphic Using VBscript
          Level 1
          ' For I = 1 to cInt(myMountNumber)<br />For I = 1 to myDoc.TextFrames.Counts<br />' I think your myMountNumber is equal to TextFramesCount<br /><br />' Set myTextFrames = myDocument.TextFrames<br />' Set myCurrentFrame = myTextFrames.Item(I)<br />' you don't need to get reference to ALL TextFrames in EACH iteration<br /><br />Set myCurrentFrame = myDocument.TextFrames.Item(I)<br />' get reference to Ith TextFrame in document<br /><br />' If myCurrentFrame.Texts.ItemByRange (1,7).Contents = "<Place:" Then<br />If left(myCurrentFrame.Texts.Item(1).Contents,7) = "<Place:" Then<br />' looks better and should be faster<br /><br />   ' Set myLastCharacter = myCurrentFrame.Characters.Item(-2)<br />   ' myCurrentFrame.Texts.ItemByRange (1,myLastCharacter).Item(1).Contents = ""<br />   ' you don't need these lines - you will delete contents in PLACE line<br />   <br />   ' myCurrentFrame.InsertionPoints.Item(1).Place ("E:\sxs\pm65\Sundco.jpg")<br /><br />   myFile = Mid(myCurrentFrame.Texts.Item(1),8)<br />   ' get all after "<Place:"<br /><br />   myFile = Split(myFile,">")(0) ' get all to ">"<br />   ' small trick ;) when split - get array - but we can use (0) to refere to first element<br /><br />   myCurrentFrame.Texts.Item(1).Place(myFile)<br />   myCurrentFrame.Fit idFitOptions.idProportionally<br />End If<br /><br />Next<br /><br />and I don't think that iterating TextFrames collection is good way ...<br />you should iterate Stories collection and refer to first TextFrame - unles you are sure what you are doing ;) and you have Stories where each TextFrame will have image inside ...<br /><br />robin<br /><br />-- <br />www.adobescripts.com
          • 2. Re: Finding File Name And Using It To Place The Graphic Using VBscript
            Level 1
            Hi Robin,
            The reason for the myMountNumber is the script will need to only loop thru part of the document (the document will contain threaded frames for batch number, date, time, etc. in addition to the rubber stamps). Below is what I currently have after I added your changes, but the one problem is the graphic is also removing the next frame insert character that follows the file name. What would be the change to fix that issue?
            Thanks,
            Archie

            Rem Graphic Test

            Dim myInDesign
            Set myInDesign = CreateObject("InDesign.Application.CS3")

            Set FileSys = CreateObject("Scripting.FileSystemObject")

            Rem SET VALUES
            Rem ----------
            Dim myMountSize
            Dim myTemplateFolder

            REM LINE:13
            myMountNumber = "3"

            Set myDocument = myInDesign.ActiveDocument

            Rem LOOP
            Rem ----

            For I = 1 to cInt(myMountNumber)

            Set myCurrentFrame = myDocument.TextFrames.Item(I)

            MsgBox myCurrentFrame.Texts.Item(1).Contents
            REM LINE: 26
            If left(myCurrentFrame.Texts.Item(1).Contents,7) = "<place=" Then
            myFile = Mid(myCurrentFrame.Texts.Item(1).Contents,8)
            myFile = Split(myFile,">")(0)' get all to ">"
            myCurrentFrame.Texts.Item(1).Place (myFile)
            myCurrentFrame.Fit idFitOptions.idProportionally
            End If

            Next

            MsgBox "Complete:"
            • 3. Re: Finding File Name And Using It To Place The Graphic Using VBscript
              Level 1
              Hi Archie,

              You might want to take a look at the example script ReplaceTextWithGraphic.vbs that's in the archive of scripts that go with the Scripting Guide--you can find the archive at:

              http://www.adobe.com/products/indesign/scripting/index.html

              Click the Scripting Resources tab to get to the scripting info. The Zip archive associated with the Scripting Guide contains over 200 VBScript examples.

              Thanks,

              Ole
              • 4. Re: Finding File Name And Using It To Place The Graphic Using VBscript
                Level 1
                Hi Ole,

                I looked at that script, but I couldn't find what I was looking for. I made the changes below and now it is working, not sure if it's the best method. For the test I created 3 text frames, starting from the top of the page, then I created a thread, beginning with the frame at the top. When I put a message box in the loop I found out that it was starting from the last frame. How can I control the frame order when I create a template?

                Thanks,
                Archie

                Rem Graphic Test

                Dim myInDesign
                Set myInDesign = CreateObject("InDesign.Application.CS3")

                Set FileSys = CreateObject("Scripting.FileSystemObject")

                Rem SET VALUES
                Rem ----------
                Dim myMountSize
                Dim myTemplateFolder

                myMountNumber = "3"

                Set myDocument = myInDesign.ActiveDocument

                Rem LOOP
                Rem ----

                For I = 1 to cInt(myMountNumber)

                Set myCurrentFrame = myDocument.TextFrames.Item(I)

                If left(myCurrentFrame.Texts.Item(1).Contents,7) = "<place=" Then
                myFile = Mid(myCurrentFrame.Texts.Item(1).Contents,8)
                myFile = Split(myFile,">")(0)' get all to ">"
                Set myLastCharacter = myCurrentFrame.Characters.Item(-2)
                MyCurrentFrame.Texts.ItemByRange (1,myLastCharacter).Item(1).Contents=""
                myCurrentFrame.InsertionPoints.Item(1).Place (myFile)
                myCurrentFrame.Fit idFitOptions.idProportionally
                End If

                Next
                • 5. Re: Finding File Name And Using It To Place The Graphic Using VBscript
                  Level 1
                  change line:<br /><br />  Set myLastCharacter = myCurrentFrame.Characters.Item(-2)<br /><br />to:<br /><br />  Set myLastCharacter = myCurrentFrame.Characters.Item(7 + Len(myFile) + 1)<br /><br />7 - lenght of "<place="<br />1 - lenght of ">"<br /><br />robin<br /><br />-- <br />www.adobescripts.com
                  • 6. Re: Finding File Name And Using It To Place The Graphic Using VBscript
                    Level 1
                    Hi Robin,

                    Thanks for your help. I below is my current version. My next issues is if the graphic is larger than the frame. In this case I need the graphic to be reduced to fit in the frame. The fit commands do not seem to work, I do not have them in the correct format. If I make a frame contents set for a graphic (not using a script) I can use the fit commands on the menu to work correctly. Would it make sense to create a new frame (same size and position) set for graphic content and place the image in that frame?

                    Thanks,
                    Archie

                    Dim myInDesign
                    Set myInDesign = CreateObject("InDesign.Application.CS3")

                    Set FileSys = CreateObject("Scripting.FileSystemObject")

                    Rem SET VALUES
                    Rem ----------
                    Dim myMountSize
                    Dim myTemplateFolder

                    myMountNumber = "3"

                    Set myDocument = myInDesign.ActiveDocument

                    myPlaceGraphics myDocument, myMountNumber

                    MsgBox "Completed:"

                    Rem PLACE GRAPHICS
                    Rem --------------
                    Function myPlaceGraphics (myDocument, myMountNumber)

                    For I = 1 to cInt(myMountNumber)
                    REM For I = 4 to myDocument.TextFrames.Count

                    Set myCurrentFrame = myDocument.TextFrames.Item(I)
                    myCharacterCount = MyCurrentFrame.Characters.Count
                    If myCharacterCount > 0 Then
                    If left(myCurrentFrame.Texts.Item(1).Contents,7) = "[place=" Then
                    myFile = Mid(myCurrentFrame.Texts.Item(1).Contents,8)
                    myFile = Split(myFile,"]")(0)' get all to "]"
                    Set myLastCharacter = myCurrentFrame.Characters.Item(7 + Len(myFile) + 1)
                    myCurrentFrame.Texts.ItemByRange (1,myLastCharacter).Item(1).Contents=""
                    myCurrentFrame.InsertionPoints.Item(1).Place (myFile)
                    myCurrentFrame.Fit idFitOptions.idProportionally
                    myCurrentFrame.Fit idFitOptions.idContentToFrame
                    End If
                    End If

                    Next

                    End Function