首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Office将customProperty添加到新文档中

Office将customProperty添加到新文档中
EN

Stack Overflow用户
提问于 2018-05-10 14:39:22
回答 1查看 409关注 0票数 2

我正在为办公室(word)开发一个addIn (Word),我一直在处理这个问题。我需要为将在新窗口/实例中打开的新文档分配自定义属性。

我已经对已经以这种方式打开的文档使用了自定义属性:

代码语言:javascript
复制
setProperty(propName, propValue) {
 Word.run(context => {
  context.document.properties.customProperties.add(propName, propValue);
  return context.sync();
}).catch(error => {
  if (error instanceof OfficeExtension.Error) {
    console.log(
      "Error code and message: " + JSON.stringify(error.debugInfo)
    );
   }
 });
}

getFileOverride(attach, file) {
 self.getDocumentAsBase64(attach.id).then(data => {
  Word.run(context => {
    let body = context.document.body;
    body.insertFileFromBase64(data, Word.InsertLocation.replace);
    return context
      .sync()
      .then(() => {
        self.setProperty("fileId", file.id);
        self.setProperty("attachId", attach.id);
      })
      .then(context.sync)
      .catch(error => {
        self.updateStatus("Error al obtener el archivo");
        if (error instanceof OfficeExtension.Error) {
          console.log(
            "Error code and message: " + JSON.stringify(error.debugInfo)
          );
        }
      });
  }).catch(error => {
    if (error instanceof OfficeExtension.Error) {
      console.log(
        "Error code and message: " + JSON.stringify(error.debugInfo)
      );
    }
  });
 });
}

但是当我创建一个新的文档时,我不知道如何实现这一点。我尝试了以下方法,但是给出了一个一般的异常错误:

代码语言:javascript
复制
getDocumentAsBase64(function(data) {
 Word.run(function(context) {
  var myNewDoc = context.application.createDocument(data);
  context.load(myNewDoc);

  return context
    .sync()
    .then(function() {
      myNewDoc.properties.load();
      myNewDoc.properties.customProperties.add("custom", "prop");
      myNewDoc.open();
    })
    .then(context.sync)
    .catch(function(myError) {
      //otherwise we handle the exception here!
      updateStatus(myError.message);
    });
 }).catch(function(myError) {
   updateStatus(myError.message);
 });
});

我尝试过创建一个类似于setProperty的函数,但它没有添加以下属性:

代码语言:javascript
复制
function setExternalProperty(document, propName, propValue) {
 Word.run(context => {
 document.properties.load();
 document.properties.customProperties.add("custom", "prop");
 return context.sync();
}).catch(error => {
  if (error instanceof OfficeExtension.Error) {
   console.log("Error code and message: " + JSON.stringify(error.debugInfo));
  }
 });
}

我怎样才能做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-10 17:11:25

我找到了解决办法,很简单。我将我的职能改为:

代码语言:javascript
复制
getFileNew(attach, file) {
 self.getDocumentAsBase64(attach.id).then(data => {
  Word.run(context => {
    var myNewDoc = context.application.createDocument(data);
    myNewDoc.properties.load();
    myNewDoc.properties.customProperties.add("fileId", file.id);
    myNewDoc.properties.customProperties.add("fileName", file.name);
    myNewDoc.properties.customProperties.add("attachId", attach.id);
    myNewDoc.properties.customProperties.add("attachName", attach.name);
    myNewDoc.open();
    return context.sync()
  }).catch(error => {
    if (error instanceof OfficeExtension.Error) {
      console.log(
        "Error code and message: " + JSON.stringify(error.debugInfo)
      );
    }
  });
 });
}

SIDENOTE:这只适用于桌面版本的。如果要在Office的新窗口中打开文档,则必须省略customProperties,否则会引发异常

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50275496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档