ms-wordms-officeoffice-js

Office JS - Add customProperty to new document


I´m developing an addIn for office (word) and I´m stuck with this problem. I need to assign custom properties to a new document that is going to be opened in a new window/instance.

I´m already using custom properties for documents that are already opened this way:

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)
      );
    }
  });
 });
}

But when I create a new document I don´t know how to achieve this. I´ve tried the following but gives a General Exception error:

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);
 });
});

I've tried making a function similar to setProperty but it doesn't add the properties:

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));
  }
 });
}

How can I achieve this?


Solution

  • I found the solution, it was quite simple. I change my function to this:

    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: This only works on desktop version. If you want to open a document in a new window in Office Online you have to omit the customProperties or it´ll throw an exception