无法在Word的桌面版本中更新标题

时间:2018-06-24 12:09:33

标签: ms-word office-js office-addins

我有一个Office插件,正在尝试在桌面上更新文档的标题。我尝试了2种不同的方式,但它们都无法在台式机上使用。它可以在线上运行,但不能在桌面上运行。

Word.run(async (context) => {

   var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
   console.log(newTitle.value);
   context.document.properties.title = newTitle.value;

   });

此代码可在线运行,但不能在桌面上运行。我也尝试过这样做。

Office.context.document.customXmlParts.getByNamespaceAsync("http://schemas.openxmlformats.org/package/2006/metadata/core-properties", 
function (resultCore) {

   var xmlPart = resultCore.value[0];
   xmlPart.getNodesAsync('*/*', function (nodeResult) {

      for (var i = 0; i < nodeResult.value.length; i++) {

       var node = nodeResult.value[i];
       console.log("BaseName: ")
       console.log(node.baseName);

           if (node.baseName === "title") {

              var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
              console.log("title that you entered: " + newTitle.value);
              console.log(node);

              node.setNodeValueAsync(newTitle.value, { asyncContext: "StateNormal" }, function (data) { });

       }

     }

   });
});

有人知道为什么它不起作用或对我的问题有其他解决方案吗?

1 个答案:

答案 0 :(得分:0)

以下代码对我适用,包括在台式机上。请注意,您必须await Word.run。另外,您必须加载标题,然后进行同步,以确保已更改了实际文档上的标题,而不仅仅是在任务窗格代码中的代理对象中。

await Word.run(async (context) => {
    var newTitle = document.getElementById("inputTitle") as HTMLInputElement;
    console.log(newTitle.value);
    context.document.properties.title = newTitle.value;
    const myProperties = context.document.properties.load("title");
    await context.sync();
    console.log(myProperties.title);
});
相关问题