Could someone provide a simple example of getting DocumentProperties in Office.js?

Viewed 555

How do I get the author or title of a Word document using Office.js (1.3)?

I read the documentation on documentProperties but I need an example to get the syntax right.

Help appreciated!

1 Answers

The following code snippet gets the author and title of the document and writes those property values to the console.

Word.run(function (context) {
  var document = context.document;
  document.properties.load("author, title");
  
  return context.sync()
    .then(function () {
      console.log("The author of this document is " + document.properties.author + " and the title is '" + document.properties.title + "'");
    });
});

Note that with the Office.js APIs, you must call the load() method on an object to explicitly load the property value before you'll be able to read it. (You can find information about the load() method in the same article you linked to in your question.)

Related