XMetaL Home Page XMetaL Community Forum
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 07, 2010, 11:41:21 AM
2810 Posts in 830 Topics by 1931 Members
Latest Member: warepublic
XMetaL Community Forums  |  General  |  General XMetaL Discussion  |  Pasting xml from another file via macro, should that cause problems? « previous next »
Pages: 1
Author Topic: Pasting xml from another file via macro, should that cause problems?  (Read 516 times)
russurquhart1
Member

Posts: 23


« on: February 08, 2010, 04:36:37 PM »

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!

Thanks,


Russ

-------------------


<MACRO name="OMAPBR" key="" lang="JScript"><![CDATA[
if (Selection.CanInsert("SubSection")) {
       Selection.InsertElement ("SubSection");
   Documents.OpenTemplate ("C:\\DocZone\\Template\\OMAPBRTableSet.xml");
        ActiveDocument.ViewType = 1;
   Selection.SelectElement();
   Selection.MoveLeft();
   Selection.SelectContainerContents();
        Selection.Copy();
   ActiveDocument.Close();
   Selection.Paste();
  } else {
     Application.Alert("SubSection not allowed here.");
  }
]]></MACRO>
Logged
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 831



WWW
« Reply #1 on: February 08, 2010, 06:13:26 PM »

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:
Selection.PasteString(tempString);

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."
Logged

Derek Read, Program Manager (XMetaL)
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 831



WWW
« Reply #2 on: February 08, 2010, 06:31:39 PM »

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).
« Last Edit: February 08, 2010, 06:34:14 PM by Derek Read » Logged

Derek Read, Program Manager (XMetaL)
Pages: 1
XMetaL Community Forums  |  General  |  General XMetaL Discussion  |  Pasting xml from another file via macro, should that cause problems? « previous next »
Jump to: