Skip to main content
K.Daube
Community Expert
Community Expert
February 26, 2018
Answered

Invalid text location

  • February 26, 2018
  • 2 replies
  • 595 views

Dear all,

Int he following script I want to place a variable at a particular position in a paragraph.

Obviously my method to define a TextLocation in line 11 is wrong. Line 26is never executed and hence no variable inserted.

TestInsertVariable ();

function TestInsertVariable () {
  var paraText = "Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts."
  var  oDoc = app.ActiveDoc, sValue, sVarName, oTextLoc, oPgf;

  if(oDoc.ObjectValid()) {
    oPgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
    oTextLoc = new TextLoc(oPgf, 0);
    oDoc.AddText(oTextLoc, paraText);              // create a paragraph
    oTextLoc = new TextLoc(oPgf, 17);              // arbitrary place
    sVarName = "GugusVariable";
    sValue   = "--- Anything I want to see in my variable ---";
    InsertVariable (oDoc, oTextLoc, sVarName, sValue);

  } else {
    alert ("Document does not exist or is invalid");
  }
} //--- end TestInsertVariable

function InsertVariable (oDoc, oTextLoc, sVarName, sValue) { //=== Insert variable at location ====
var oVar;
$.bp(true);
  oVar = oDoc.NewAnchoredFormattedVar (sVarName, oTextLoc);
  if (oVar.ObjectValid ()) {
    oVar.Fmt = sValue;
  }
} //--- end InsertVariable

Do I need the detour with TextSelection, etc. ?

This topic has been closed for replies.
Correct answer Klaus Göbel

Hi Klaus,

you just forgot to create the variable in the second function

function InsertVariable (oDoc, oTextLoc, sVarName, sValue) { //=== Insert variable at location ==== 

    var oVar; 

   

    var loCellVar = oDoc.NewNamedVarFmt(sVarName);  // NEW

    loCellVar.Fmt = sValue; // NEW

   

    oVar = oDoc.NewAnchoredFormattedVar (loCellVar.Name, foTextLoc);  // CHANGED FIRST PARAM to loCellVar.Name

    

   /*

       You don't need that

      if (oVar.ObjectValid())

        { 

        oVar.Fmt = sValue; 

        } 

*/

    } //--- end InsertVariable 


    } //--- end InsertVariable 

2 replies

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 26, 2018

Thanks, Klaus!

I forgot that I do not insert an already known variable, but also need to define the new variable …

Klaus Göbel
Klaus GöbelCorrect answer
Legend
February 26, 2018

Hi Klaus,

you just forgot to create the variable in the second function

function InsertVariable (oDoc, oTextLoc, sVarName, sValue) { //=== Insert variable at location ==== 

    var oVar; 

   

    var loCellVar = oDoc.NewNamedVarFmt(sVarName);  // NEW

    loCellVar.Fmt = sValue; // NEW

   

    oVar = oDoc.NewAnchoredFormattedVar (loCellVar.Name, foTextLoc);  // CHANGED FIRST PARAM to loCellVar.Name

    

   /*

       You don't need that

      if (oVar.ObjectValid())

        { 

        oVar.Fmt = sValue; 

        } 

*/

    } //--- end InsertVariable 


    } //--- end InsertVariable 

frameexpert
Community Expert
Community Expert
February 26, 2018

You can make this a little more general with a "helper" function:

function getVarFmt (varName, varDef, doc) {

  

    // Get a variable format from the document; if it doesn't exist, create it.

    var varFmt = doc.GetNamedVarFmt (varName);

    if (varFmt.ObjectValid () === 0) {

        varFmt = doc.NewNamedVarFmt (varName);

        varFmt.Fmt = varDef;

    }

    return varFmt;

}

This ensures that, if the variable format already exists, it will be used; if it doesn't exist, it will be created. In the function above, I am passing in a "varDef" argument but that won't be used if the variable format already exists; it will only be used if the function has to create the format. In the example below, I don't pass in a definition argument, but set a generic string if the cross-reference format doesn't exist (line 08):

function getXRefFmt (name, doc) {

   

    var xrefFmt;

   

    xrefFmt = doc.GetNamedXRefFmt (name);

    if (!xrefFmt.ObjectValid ()) {

        xrefFmt = doc.NewNamedXRefFmt (name);

        xrefFmt.Fmt = "*** XRefFmt needs to be defined ***";

    }

    return xrefFmt;

}

www.frameexpert.com
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 26, 2018

Rick, thanks for this additional information.

I already expanded my InsertVariable function analogous to your getVarFmt - the variable may be already defined or needs to be defined.

I'll put Your examples into my 'private database'.

Klaus