Ms word insertContentControl()上的GeneralException

Ms word insertContentControl()上的GeneralException,ms-word,office-js,Ms Word,Office Js,我对搜索返回的部分但不是大部分范围的insertContentControl有问题 本质上,我搜索各种术语,然后为每个查找创建一个内容控件。这基本上是可行的,但我经常会遇到这样的一般性异常: {code: "GeneralException", message: "Sorry, something went wrong. Check the OfficeExtension.Error.debugInfo for more information. ", e

我对搜索返回的部分但不是大部分范围的insertContentControl有问题

本质上,我搜索各种术语,然后为每个查找创建一个内容控件。这基本上是可行的,但我经常会遇到这样的一般性异常:

{code: "GeneralException", message: "Sorry, something went wrong. Check the OfficeExtension.Error.debugInfo for more information. ", errorLocation: "Range.insertContentControl", statement: "var insertContentControl=v.insertContentControl();", toString: ƒ, …}
code: "GeneralException"
errorLocation: "Range.insertContentControl"
fullStatements: (7) ["var v=context.root._getObjectByReferenceId("{4a645…31b06a1e8cb}{205}") /* originally getItem(28) */;", "var insertContentControl=v.insertContentControl();", "// Instantiate {insertContentControl}", "insertContentControl.title="TypeOne";", "insertContentControl.color="yellow", "insertContentControl.tag="someTag";", "insertContentControl.appearance="Tags";"]
message: "Sorry, something went wrong. Check the OfficeExtension.Error.debugInfo for more information. "
statement: "var insertContentControl=v.insertContentControl();"
surroundingStatements: Array(9)
0: "var v=context.root._getObjectByReferenceId("{4a64573e-3807-4278-a22d-131b06a1e8cb}{205}") /* originally getItem(...) */;"
1: "// >>>>>"
2: "var insertContentControl=v.insertContentControl();"
3: "// <<<<<"
4: "// Instantiate {insertContentControl}"
5: "insertContentControl.title=...;"
6: "insertContentControl.color=...;"
7: "insertContentControl.tag=...;"
8: "insertContentControl.appearance=...;"
lastIndex: (...)
lastItem: (...)
length: 9
我主要是在Word Online上测试,但Word desktop似乎显示了相同的问题


我就是找不到原因。我还能做些什么来找到问题的根源呢?

正如Cindy所建议的,我可以修改您的脚本,在出现第一个异常时停止并找出发生了什么。好小费,辛迪

此图立即显示了您试图在红色范围内插入与现有CC重叠的CC的问题,这是一个无效操作:


使用API确定您处于这样的情况。

对于给定文档,它是否总是在相同的范围内出错,还是可变?此外,这不会解决您的问题,但不会加载parentContentControlOrNullObject。你永远不需要加载方法。另外,如果你增加搜索词的数量或文档的大小,问题会变得更糟吗?Hello@RickKirkham-是的,它总是在相同的条目上抛出。我不确定第二个问题。。。我想是的,但只是因为出错的可能性更大。我还没有机会回到这个问题上来。也许几天后吧。
//This object may contain thousands of terms across multiple entity types
let entities = {
    EntityType1:{
            Term1: [],
            Term2: []
    }
}

await Word.run(async (context) => {    
    
    // search for terms
    for(let entityKey in entities) {    
        let terms = entities[entityKey]
        for(let termKey in terms){

            let searchResults = context.document.body.search(termKey, {matchWholeWord:true, matchCase: true});
            searchResults.load(["text", "parentContentControlOrNullObject"]);

            _.extend(terms[termKey], ({"searchHits": searchResults}))
        }
    }

    await context.sync()

    //add content controls for each found term
    
    for(let entityKey in entities) {    
        let terms = entities[entityKey]

        for(let termKey in terms){
            let term = terms[termKey];
            let searchHits = term.searchHits.items;

            for (const hit of searchHits) {

                //this always works
                //hit.font.highlightColor = EntityColors[entityKey]

                try {
                    let cc:Word.ContentControl = hit.parentContentControlOrNullObject

                    if(cc.isNullObject || cc.tag != appId){
                        cc = hit.insertContentControl()
                    }

                    cc.title = entityKey
                    cc.color = "yellow"
                    cc.tag = "someTag"
                    cc.appearance = "Tags"

                    //NB. this is here only for debugging - it really slows everything down
                    await context.sync()  

                } catch (error) {
                    if (error instanceof OfficeExtension.Error) {
                        console.log(error.debugInfo)
                        console.log(error.innerError)
                    }
                }
            }
        }
    }

    await context.sync()

})