General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Pasting xml from another file via macro, should that cause problems?

  • russurquhart1

    Pasting xml from another file via macro, should that cause problems?

    Participants 1
    Replies 2
    Last Activity 12 years, 11 months ago

    The macro below, when executed, pastes some tables from an xml template file. It SEEMS that right after this, when selecting the name of the table, for example, Xmetal blows up. Is this a known bug?

    Is there something wrong with this method, or is there a better way?

    Any help is greatly appreciated!




    if (Selection.CanInsert(“SubSection”)) {
        Selection.InsertElement (“SubSection”);
    Documents.OpenTemplate (“C:\DocZone\Template\OMAPBRTableSet.xml”);
            ActiveDocument.ViewType = 1;
      } else {
      Application.Alert(“SubSection not allowed here.”);


    Derek Read

    Reply to: Pasting xml from another file via macro, should that cause problems?

    It is possible that the issue is stemming from the fact that you are trying to paste from a Selection object that no longer exists (because you've closed the document just beforehand). For example, you might not have just text on the clipboard, but instead a reference to a Selection object in the file C:\DocZone\Template\OMAPBRTableSet.xml, but because that file has been closed the object doesn't exist anymore.

    Can you try modifying the script to first create string variable, like so:
    var tempString = "";

    …then place the content of what you copy in there as a string:
    tempString = Selection.Text;

    …then paste the content of that string var instead:

    I'm not sure if this is the real issue or not, just a guess.

    Normally if I was doing something like this I would probably open the XML file (OMAPBRTableSet.xml) you are copying from invisibly, either purely as a string (less overhead but then you need to use pure JScript string functions to locate your content) or in another parser like MSXML (more overhead but then you get MSXML DOM and XPath functions for locating content). That way the user never sees the file open. I would also normally use a Range instead as the user does not see those when manipulating document content.

    I assume your example is simplified. But just in case, you might want to put in some additional tests (that is assuming you still want to open the template in XMetaL to copy from it):

    1) Test if the fileing opened actually exists (if it doesn't the rest of your code will end up possibly copying and pasting from the current document instead).
    if (Application.FileExists("C:\DocZone\Template\OMAPBRTableSet.xml") {...

    2) Test if opening the file succeeded.
    var tempDoc = Documents.OpenTemplate("C:\DocZone\Template\OMAPBRTableSet.xml");
    if (tempDoc) {...

    3) Test that you have copied the type of content you really wanted after the line where you switch to ViewType = 1. I'm not sure what exact test would be needed here, and you might get away without it if you want to assume the file will never change (then why copy from the file to begin with…?) but it just seems like a good place to test for something.

    4) Perhaps one more test (in lieu of test 3) that checks using Selection.CanPaste() just in case what was copied to the string still cannot go in for some reason. This should give you a chance to give a more meaningful error than the default, which might only be the system error “beep” or “The paste operation cannot be completed even with rules checking turned off.”


    Derek Read

    Reply to: Pasting xml from another file via macro, should that cause problems?

    Something else your code does that mine does not do is override what is on the clipboard. This might be intentional with your code, not sure. In my case I would only place something on the clipboard with script if the user knew beforehand that this was going to be the case (ie: some string they could then use in another application for example). Otherwise, you may have angry users if you delete their data (whatever they had on the clipboard prior to running the script).


  • You must be logged in to reply to this topic.

Lost Your Password?