dnmh
dnmh

Reputation: 2135

Office.js recognizing ContentControl positions in the document

I am not sure this is doable out of the box, but this is what I have:

No issues there. However, is there a way to know, using something inside Office.js, if two or more ContentControls are next to each other inside the document? By 'next to each other' I mean: no other text, objects etc. in between them. I am asking because I'm attempting to merge such ContentControls. That is not an issue, but recognizing them might be.

Is this possible through Office.js or do I need to write my own custom logic?

Upvotes: 2

Views: 263

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25693

There's nothing built into the Word object models that will tell you this. But you can calculate it from the Range of the content controls using the compareLocationWith method. For example: the following snippet compares the location of the first and second content controls in a document. If the first is immediately before the second the method returns AdjacentBefore, otherwise it will return Before.

    var ccs = context.document.body.contentControls;
    ccs.load("items");
    await context.sync();
    var nrCCs = ccs.items.length;

    if (nrCCs >= 2) {   
      var cc1 = ccs.items[0];
      var cc2 = ccs.items[1];
      var rng1 = cc1.getRange("Whole");
      var rng2 = cc2.getRange("Whole");
      var sCompareResult = rng1.compareLocationWith(rng2);
      await context.sync();
      console.log(sCompareResult.value);
    }

Upvotes: 4

Related Questions