Home Forums General XMetaL Discussion Pasting xml from another file via macro, should that cause problems? Reply To: Pasting xml from another file via macro, should that cause problems?

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.”