Home Forums General XMetaL Discussion Makro Search Entity and Replace it Reply To: Makro Search Entity and Replace it


Reply to: Makro Search Entity and Replace it

Is the ultimate goal for this script to replace only nbsp with a normal space?
If so perhaps we could simplify it a lot by simply doing a text replacement on the entire document as a string.

I'm suggesting this because my brain is stuck figuring out the logic from my original script. I see why it does not work but not how to fix it. It doesn't work when rng1 cannot go further than the last entity, which is what happens with your example document. GotoNext() is working as designed, but is no good for this usage as far as I can tell. I think MoveRight() might get you closer, but I think that would make the script very slow.

A script that loads the whole document into a string, replaces all the entities using regular expression matching and then replaces the entire document with the new version might do it. I wonder if that would work for your use case (not sure about your timing or other requirements). The user's current selection will be lost, so I think it only makes sense to do this either as part of something where users are not involved at all, or just after document open.

I'm much faster at JScript than VBScript so here's a JScript example:

[code]//XMetaL Script Language JScript:
var rng = ActiveDocument.Range;
var wholeDocAtRoot = rng.TextWithRM;
var rx = / /g;
var newDoc = wholeDocAtRoot.replace(rx,” “);

Sorry to bring this post back from the dead, but your code example works for me. However, it removes all of the track changes PIs on paste.

Is there any way to search/replace with regular expressions that will preserve the track changes markup?