Ms word document.contentControls不返回文档中的所有contentControls

Ms word document.contentControls不返回文档中的所有contentControls,ms-word,office365,office-js,office-addins,word-contentcontrol,Ms Word,Office365,Office Js,Office Addins,Word Contentcontrol,我有一个文档,其中有三个ContentControl对象,如下所示: <w:body> <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D"> <w:r> <w:t xml:space="preserve">Video provides a powerful way to help you </w:t> </w:r>

我有一个文档,其中有三个
ContentControl
对象,如下所示:

<w:body>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D">
        <w:r>
            <w:t xml:space="preserve">Video provides a powerful way to help you </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc1"/>
                <w:tag w:val="prove"/>
                <w:id w:val="806369342"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF9900"/>
                <w15:appearance w15:val="tags"/>
                <w:text/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>prove</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t xml:space="preserve"> your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc2"/>
                <w:tag w:val="number 2"/>
                <w:id w:val="1463999480"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF0000"/>
                <w15:appearance w15:val="tags"/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>type</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t xml:space="preserve"> a keyword to search online for the video that best fits your document.</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D">
        <w:r>
            <w:t xml:space="preserve">To make your document look professionally produced, Word provides </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc3"/>
                <w:tag w:val="xxx"/>
                <w:id w:val="1703202634"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF99CC"/>
                <w15:appearance w15:val="tags"/>
                <w:text/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>header</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t>, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D"/>
    <w:p w:rsidR="00000000" w:rsidRDefault="0075044D"/>
    <w:sectPr w:rsidR="00000000">
        <w:pgSz w:w="11906" w:h="16838"/>
        <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
        <w:cols w:space="708"/>
        <w:docGrid w:linePitch="360"/>
    </w:sectPr>
</w:body> 

以下是整个文档的标记—但本质上文档体的标记如下所示:

<w:body>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D">
        <w:r>
            <w:t xml:space="preserve">Video provides a powerful way to help you </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc1"/>
                <w:tag w:val="prove"/>
                <w:id w:val="806369342"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF9900"/>
                <w15:appearance w15:val="tags"/>
                <w:text/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>prove</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t xml:space="preserve"> your point. When you click Online Video, you can paste in the embed code for the video you want to add. You can also </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc2"/>
                <w:tag w:val="number 2"/>
                <w:id w:val="1463999480"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF0000"/>
                <w15:appearance w15:val="tags"/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>type</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t xml:space="preserve"> a keyword to search online for the video that best fits your document.</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D">
        <w:r>
            <w:t xml:space="preserve">To make your document look professionally produced, Word provides </w:t>
        </w:r>
        <w:sdt>
            <w:sdtPr>
                <w:alias w:val="cc3"/>
                <w:tag w:val="xxx"/>
                <w:id w:val="1703202634"/>
                <w:placeholder>
                    <w:docPart w:val="1F3FDE3D075A4E8AADE251C4E318E379"/>
                </w:placeholder>
                <w15:color w:val="FF99CC"/>
                <w15:appearance w15:val="tags"/>
                <w:text/>
            </w:sdtPr>
            <w:sdtContent>
                <w:r>
                    <w:t>header</w:t>
                </w:r>
            </w:sdtContent>
        </w:sdt>
        <w:r>
            <w:t>, footer, cover page, and text box designs that complement each other. For example, you can add a matching cover page, header, and sidebar. Click Insert and then choose the elements you want from the different galleries.</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="0075044D" w:rsidRDefault="0075044D"/>
    <w:p w:rsidR="00000000" w:rsidRDefault="0075044D"/>
    <w:sectPr w:rsidR="00000000">
        <w:pgSz w:w="11906" w:h="16838"/>
        <w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/>
        <w:cols w:space="708"/>
        <w:docGrid w:linePitch="360"/>
    </w:sectPr>
</w:body> 

为了方便起见,这里有一个简单的例子

有趣的是,此VBA代码返回正确的结果(
3
):


我只在Windows 10/Office 365桌面客户端上尝试过此操作

正如Word JS API的内容控件文档中所述,仅支持/识别富文本内容控件。所以代码不会“看到”纯文本内容控件


内容控件是纯文本还是富文本在视觉上无法识别(除非它包含格式或文本以外的内容)。Word Open XML中也没有区分内容控件类型的内容,除非它包含格式或非文本对象。

正如Word JS API的内容控件文档中所述,只支持/识别富文本内容控件。所以代码不会“看到”纯文本内容控件


内容控件是纯文本还是富文本在视觉上无法识别(除非它包含格式或文本以外的内容)。Word Open XML中也没有区分内容控件类型的内容,除非它包含格式或非文本对象。

正如Cindy Meister所说,Word OfficeJS API只返回RichText内容控件-请参阅

如果深入研究OOXML,您会发现纯文本上下文控件(未返回的控件)与富文本控件的主要区别在于它们被标记为
。这本质上意味着,如果删除该标记,则纯文本内容控件将变为富文本内容控件。下面是我解决这个问题的方法:

var ooxml = context.document.body.getOoxml();
await context.sync();
var newxml = ooxml.value.replace(/<w:text\/>/g, '');

context.document.body.insertOoxml(newxml, Word.InsertLocation.replace);

var myContentControls = context.document.contentControls;
myContentControls.load();
await context.sync();

console.log(myContentControls.items.length); // now returns 3. Yay!!!

return context.sync();
var ooxml=context.document.body.getOoxml();
wait context.sync();
var newxml=ooxml.value.replace(//g',);
context.document.body.insertOoxml(newxml,Word.InsertLocation.replace);
var myContentControls=context.document.contentControls;
myContentControl.load();
wait context.sync();
console.log(myContentControls.items.length);//现在返回3。耶!!!
返回context.sync();

Nota Bene
getOoxml()
insertOoxml()
是速度较慢的方法,此解决方案需要很长时间才能执行,因此仅在严格必要时使用

正如Cindy Meister所说,Word OfficeJS API只返回RichText内容控件-请参阅

如果深入研究OOXML,您会发现纯文本上下文控件(未返回的控件)与富文本控件的主要区别在于它们被标记为
。这本质上意味着,如果删除该标记,则纯文本内容控件将变为富文本内容控件。下面是我解决这个问题的方法:

var ooxml = context.document.body.getOoxml();
await context.sync();
var newxml = ooxml.value.replace(/<w:text\/>/g, '');

context.document.body.insertOoxml(newxml, Word.InsertLocation.replace);

var myContentControls = context.document.contentControls;
myContentControls.load();
await context.sync();

console.log(myContentControls.items.length); // now returns 3. Yay!!!

return context.sync();
var ooxml=context.document.body.getOoxml();
wait context.sync();
var newxml=ooxml.value.replace(//g',);
context.document.body.insertOoxml(newxml,Word.InsertLocation.replace);
var myContentControls=context.document.contentControls;
myContentControl.load();
wait context.sync();
console.log(myContentControls.items.length);//现在返回3。耶!!!
返回context.sync();

Nota Bene
getOoxml()
insertOoxml()
是速度较慢的方法,此解决方案需要很长时间才能执行,因此仅在严格必要时使用

可以肯定的是:内容控件都在文档的正文中?例如,文本框中没有任何内容?原则上我不处理下载的文件。。。如果我有这个文件,下一步我会尝试使用VBA等效程序——看看它是否有同样的问题,或者它是否“看到”了所有的内容控件。这样我们就可以知道问题是在JSAPI还是在文档中,原因可能是(可能)缺少的内容控件是“纯文本”类型的控件,而不是“富文本”。JSAPI这个词只识别“富文本”内容控件。在我的测试中,跳过了“纯文本”控件;“富文本”控件已更改。(我相信它在JSAPI文档中)在文档表面上看不到这两种类型之间的区别。但是你会发现你不能对纯文本内容控件中的字符应用格式…@Cindymister你能把你的评论作为一个答案吗,这样这个问题就可以在堆栈的统计数据中算作回答了吗?谢谢。只是想确定一下:内容控件都在文档的正文中?例如,文本框中没有任何内容?原则上我不处理下载的文件。。。如果我有这个文件,下一步我会尝试使用VBA等效程序——看看它是否有同样的问题,或者它是否“看到”了所有的内容控件。这样我们就可以知道问题是在JSAPI还是在文档中,原因可能是(可能)缺少的内容控件是“纯文本”类型的控件,而不是“富文本”。JSAPI这个词只识别“富文本”内容控件。在我的测试中,跳过了“纯文本”控件;“富文本”控件已更改。(我相信它在JSAPI文档中)在文档表面上看不到这两种类型之间的区别。但是你会发现你不能对纯文本内容控件中的字符应用格式…@Cindymister你能把你的评论作为一个答案吗,这样这个问题就可以在堆栈的统计数据中算作回答了吗?谢谢