7

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!

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
11teenth
  • 1,853
  • 1
  • 15
  • 28
  • maybe this can help a bit https://github.com/OfficeDev/office-js-snippets/blob/master/samples/word/07-custom-properties/get-built-in-properties.yaml – Slai Sep 26 '17 at 15:38

1 Answers1

5

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.)

João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • For some reason when I try to run this code, the document.properties is always undefined. I am using Office 2016 and VS2017. Do you guys know this issue? – Feng Mar 18 '18 at 19:06