Hi Mark,
The problem is that the paste is not occurring where you think. The divider line is being added at the end of the file, but your paste actions are not. In fact, I think it may be somewhat random where the pastes are occurring, because this line:
var changesTloc = new TextLoc(firstChgPgf, Constants.FV_OBJ_END_OFFSET);
...is not creating a valid text location. You can confirm this by aborting the function after:
changesDoc.TextSelection = changesTRange;
...and looking at where the insertion point is. It will probably be wherever you left it last, because the call is invalid. I can't tell you exactly why, except that it has something to do with the fact that you are using the last paragraph in the flow. There is something weird about trying to set a full-paragraph text range with the last paragraph. I'm sure it is related to how the last pgf in a flow does not show a pgf mark, but beyond that, I don't know how to do what you are trying to do with the last paragraph.
Lacking the knowledge of how to do it correctly, any time I need to do this, I create a "dummy" paragraph at the end of the flow to fill in that strange "last pgf" space, then do the work around it. At the end, I just delete the dummy pgf. I don't know if this is best practice or not, but it's the only way I ever get it to work and it does sort of mirror how you might do it manually in the GUI.
With that, I modified your script as follows and it seems to work better. Note that I also modified the logic of how to find the last paragraph of the flow by writing a separate function. Your call to "FirstTextFrameInFlow" is OK unless your doc starts to span multiple pages and/or begins with multiple empty pages. The function I wrote ensures that you truly have the last pgf in the flow.
I hope this helps.
Russ
function copyPasteChanges(doc, chgRange) {
// Select section with changebar and copy it.
doc.TextSelection = chgRange;
doc.Copy();
// Get the changes doc ready and paste the changed section into it.
var lastPgf = getLastPgf(changesDoc);
var dummyPgf = changesDoc.NewSeriesPgf (lastPgf);
var changesTloc = new TextLoc(dummyPgf, 0);
var changesTRange = new TextRange(changesTloc, changesTloc);
changesDoc.TextSelection = changesTRange;
changesDoc.Paste();
// Get to end of file again (I think)
//Don't forget that the last pgf is now the dummy pgf
lastPgf = getLastPgf(changesDoc);
var dividerPgf = changesDoc.NewSeriesPgf (lastPgf.PrevPgfInFlow);
var TLoc = new TextLoc ( dividerPgf, 0 );
changesDoc.AddText ( TLoc, "_______________________" );
dummyPgf.Delete();
}
function getLastPgf(doc)
{
var textFrame = doc.MainFlowInDoc.LastTextFrameInFlow;
var lastPgf = textFrame.LastPgf;
while(!lastPgf.ObjectValid() && textFrame.ObjectValid())
{
textFrame = textFrame.PrevTextFrameInFlow;
lastPgf = textFrame.LastPgf;
}
return lastPgf;
}