3 Replies Latest reply on Sep 10, 2008 11:06 PM by (Olav_Kvern)

    Move XMLElement VB CS2

    Dave Pepper
      I want to be able to import an XML element and move it to where my cursor is currently located. The following code does this, but there's a problem with it that I can't figure out. The script only inserts the XML element in the current insertion point if the insertion point is before all XML elements.

      For example, if I have five insertion points in a story, the script works fine if I put the cursor in insertion point 5 and run the script, then put the cursor in insertion point 4 and run the script, and so on an so forth. But if I run the script with the cursor in say insertion point 2 and then try run the script in an insertion point after the newly imported XML element, the XML element is not inserted where the cursor is located. Any ideas?

      Set MyInDesign = CreateObject("InDesign.Application.Cs2")
      Set MyDoc = MyInDesign.ActiveDocument
      MyDoc.XMLElements.Item(1).XMLElements.Item(1).ImportXML "C:\Documents and Settings\Owner\Desktop\A\Parent2.xml"
      Set Elements = MyDoc.XMLElements.Item(1).XMLElements.Item(1).XMLElements
      Set MyXMLElement = Elements.Item(Elements.Count)
      MyXMLElement.MarkUpTag = "Header"
      Set MyCursorLocation = MyInDesign.Selection.Item(1).InsertionPoints.Item(1)
      MyXMLElement.Move idLocationOptions.idBefore, MyCursorLocation
        • 1. Re: Move XMLElement VB CS2
          Level 1
          Hi David,

          In this line:

          MyXMLElement.Move idLocationOptions.idBefore, MyCursorLocation
          

          ...you're mixing apples and oranges. You can only move XML elements relative to other XML elements (that is, the object you provide to the reference parameter in the XMLElement.Move method should be an XML element), not relative to text objects.

          In truth, I think InDesign will sometimes let you get away with this, and move the element if the structure will allow it--but it's better to try to figure it out in terms of the XML structure.

          Thanks,

          Ole
          • 2. Re: Move XMLElement VB CS2
            Dave Pepper Level 1
            Thanks Ole,

            The problem is that most of the time I don't want to move it in relation to another XML object, I always want it where ever the cursor is located at the time of the script. Do you know any way to do this?
            • 3. Re: Move XMLElement VB CS2
              Level 1
              Hi David,

              It's a bit tricky to just grab an XML element and then move it to another location in the XML structure. It really depends on the structure surrounding the cursor. You might actually be better off moving the *text content* of the XML you've imported to the cursor position, and then re-tagging it. If the text has paragraph and character styles applied to it, you can simply use style to tag mapping to do most of the work for you.

              I'll think about this a bit more, and try to come up with an example.

              Thanks,

              Ole