Replace string with formatted text
Dear all - I'm back to my beloved project...
I want to replace a found string by a TextSelction (formatted text) and started with Jang’s famous function FindAndReplaceString.
Since my replacement comes from a different document (sourceDoc) I edited activeDoc to targetDoc and introduced a second document (sourceDoc).
Actually the replacePara comes from an arry where it had been placed to avoid switching back and forth between the documents of a book (where to find and replace) and the source documents. I have learnt in another function that the information on the array requires that the sourceDoc must stay open.
- Of course everything works fine until I want to insert the replacelement:
line 26 clears the found string - Since I do not insert a string, I skip lines 28 and 29 and try try line 30
- At line 30 sourceDoc is object Document and replacePare is object TextSelection. However, sourceDoc.replacePara is undefined and
- (as a consequence ?) line 31 pasts the current clipboard contents.
Obviously there is some fog around me ... and I need some sunshine.
function FindAndReplacePara (targetDoc, findString, sourceDoc, replacePara, loopMax) {
var tr = new TextRange();
var restoreTR, frame = 0, loopCounter = 0, replacementCounter = 0;
var findParams = new PropVals();
var firstPgf = targetDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
tr.beg.obj = tr.end.obj = firstPgf; // set up the starting text range as the very beginning
tr.beg.offset = tr.end.offset = 0; // of the flow. We'll move straight from beginning to end.
trSaved = tr // to come back after work
findParams = AllocatePropVals(2);
findParams[0].propIdent.num = Constants.FS_FindText;
findParams[0].propVal.valType = Constants.FT_String;
findParams[0].propVal.sval = findString;
findParams[1].propIdent.num = Constants.FS_FindCustomizationFlags;
findParams[1].propVal.valType = Constants.FT_Integer;
findParams[1].propVal.ival = Constants.FF_FIND_CONSIDER_CASE;
FA_errno = Constants.FE_Success; // errno global, to be used to track the progress of the find and replace
tr = targetDoc.Find(tr.beg, findParams); // and do an initial find to get started.
while(FA_errno === Constants.FE_Success && loopCounter++ < 2*loopMax) { //find and replace loop as long as we keep finding
targetDoc.TextSelection = tr; // set up the text range to clear the original text
targetDoc.Clear(0); // clear it
// targetDoc.AddText(tr.beg, replacePara); // insert the new text at the original beginning of the text range
// tr.beg.offset += replacePara.length; // lets jimmy the text range in memory to place it directly after
targetDoc.TextSelection = sourceDoc.replacePara; // paste the whole replacement paragraph
targetDoc.Paste (0); // <-- Current contents of clipboard is pasted !!!!
if(FA_errno === Constants.FE_Success) { // increment our return counter
replacementCounter++;
}
FA_errno = Constants.FE_Success; // ... find the next instance. We'll reset FA_errno again just in case
tr = targetDoc.Find(tr.beg, findParams); // something screwy happened while we were replacing text.
}
targetDoc.ScrollToText(trSaved); // we're done. Restore the document to it's original area of display
return replacementCounter;
} // --- end FindAndReplacePara

