最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Word Add-In Checkbox Content Controls with office.js - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论