I have a Word DOCX with several Content Controls in it which I want to access via JS. For this, I have the following routine:
await Word.run(async (context) => {
const doc = context.document;
const contentControls = doc.contentControls;
contentControls.load("items, tag, type");
await context.sync();
console.log("contentControls:", contentControls);
contentControls.items.forEach(control => {
console.log("Tag:", control.tag, "Type:", control.type);
});
});
This routine correctly logs all RichText content controls in the DOCX - but none of the content controls of type Checkbox (inserted via the Developer/ Checkbox ribbon entry in Word) show up :-(
Does anyone have a clue why checkboxes are not in the doc.contentControls collection? And how they can be accessed using the office.js javascript library?
I have a Word DOCX with several Content Controls in it which I want to access via JS. For this, I have the following routine:
await Word.run(async (context) => {
const doc = context.document;
const contentControls = doc.contentControls;
contentControls.load("items, tag, type");
await context.sync();
console.log("contentControls:", contentControls);
contentControls.items.forEach(control => {
console.log("Tag:", control.tag, "Type:", control.type);
});
});
This routine correctly logs all RichText content controls in the DOCX - but none of the content controls of type Checkbox (inserted via the Developer/ Checkbox ribbon entry in Word) show up :-(
Does anyone have a clue why checkboxes are not in the doc.contentControls collection? And how they can be accessed using the office.js javascript library?
Share Improve this question edited Mar 9 at 15:05 ak2002 asked Mar 9 at 15:03 ak2002ak2002 13 bronze badges1 Answer
Reset to default 0I think I figured it out - instead of just reading the document's contentControls collection via contentControls = context.document.contentControls; the checkboxes show up when using the getContentControls function:
await Word.run(async (context) => {
const doc = context.document;
let contentControls = context.document.getContentControls({
types: [Word.ContentControlType.checkBox, Word.ContentControlType.richText]
});
contentControls.load("items, tag, type");
await context.sync();
console.log("contentControls:", contentControls);
contentControls.items.forEach(control => {
console.log("Tag:", control.tag, "Type:", control.type);
});
});
Here's the Microsoft documentation for that:
https://learn.microsoft/en-us/javascript/api/word/word.document?view=word-js-preview#word-word-document-getcontentcontrols-member(1)
And this was the stackoverflow post that set me on the right track:
Office.js Word API not able to read plain text type content controls