4

While trying to fetch Office 2016 Word document body using Office 2016 Word Add-In, java Script API methods body.getHtml() and body.getOoxml() are not working-; It is returning error "Debug info: {"errorLocation":"Body.getHtml"}"

Reference document -: http://dev.office.com/reference/add-ins/word/body

Here's my code-:

Word.run(function (context) {

            // Create a proxy object for the document body.
            var body = context.document.body;

            // Queue a commmand to get the HTML contents of the body.
            var bodyHTML = body.getHtml();

            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                $('#output').text("Body HTML contents: " + bodyHTML.value);
            });
        })
        .catch(function (error) {
            $('#output').text("Error: " + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                $('#output').text("Debug info: " + JSON.stringify(error.debugInfo));
            }
        });

Am I missing something here?

Complete Error Object-: {"name":"OfficeExtension.Error","code":"AccessDenied","message":"AccessDenied","traceMessages":[],"debugInfo":{"errorLocation":"Body.getHtml"},"stack":"AccessDenied: AccessDenied\n at Anonymous function (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:150094)\n at yi (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163912)\n at st (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163999)\n at d (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163819)\n at c (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:162405)"}

Error Code 5009 -> The add-in does not have permission to call the specific API.

Already Tried -: https://github.com/OfficeDev/office-js-snippet-explorer/issues/13 No success yet!

anatol
  • 791
  • 9
  • 16
Kshitij
  • 123
  • 8

1 Answers1

0

Are you using the API Tutorial App in Word to try this sample or have you written your own app? I just tried the sample in the API Tutorial App (which is available from the Store) and it works just fine.

// Run a batch operation against the Word object model.
Word.run(function (context) {

    // Create a proxy object for the document body.
    var body = context.document.body;

    // Queue a commmand to get the HTML contents of the body.
    var bodyHTML = body.getHtml();

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log("Body HTML contents: " + bodyHTML.value);
    });
})
.catch(function (error) {
    console.log("Error: " + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
Philip Rueker
  • 948
  • 5
  • 15
  • I am trying to create my own app!! – Kshitij Jun 16 '16 at 06:45
  • This is the Complete Error Object: **{"name":"OfficeExtension.Error","code":"AccessDenied","message":"AccessDenied","traceMessages":[],"debugInfo":{"errorLocation":"Body.getHtml"},"stack":"AccessDenied: AccessDenied\n at Anonymous function (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:150094)\n at yi (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163912)\n at st (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163999)\n at d (https://appsforoffice.microsoft.com/lib/1.1/hosted/word-win32-16.00.js:19:163819) ..** – Kshitij Jun 16 '16 at 10:59
  • 1
    where I can find this sample app which you mentioned in your answer? – Kshitij Jun 17 '16 at 10:03
  • 2
    @PhilipRueker I'm trying to get the paragraphs or all document body as an HTML content, but the text is coming break. How to resolve this and get all content as HTML? – Diego Borges Aug 13 '18 at 18:23
  • I ran this and got a Promise :( it printed nothing – Nathan B Jul 20 '20 at 06:44