dnord
dnord

Reputation: 1742

Retrieve entire Word document in task pane app / office.js

Working in Word 2013 (desktop) and office.js, we see some functionality around the user's selection (GetSelectedDataAsync, SetSelectedDataAsync), but nothing that might let you view the entire (OpenXML) document. Am I missing something?

Upvotes: 2

Views: 1879

Answers (2)

Leo
Leo

Reputation: 5122

This is not exactly what you asked for (it is only the body of the document) but it helped me... So I post it here as it is where I landed when I googled my problem.

The documentation here: https://dev.office.com/reference/add-ins/word/body suggests that getOoxml() will get you the body of the document. There is also the property text which will return you the plain text content.

The way this API works is not overly straight forward - however the examples in the online doc really help in getting started.

All the best,

Upvotes: 0

Alex Sanséau
Alex Sanséau

Reputation: 8750

Office.context.document.getFileAsync will let you get the entire document in a choice of 3 formats:

  • compressed: returns the entire document (.pptx or .docx) in Office Open XML (OOXML) format as a byte array
  • pdf: returns the entire document in PDF format as a byte array
  • text: returns only the text of the document as a string. (Word only)

Here's the example taken from MSDN:

var i = 0;
var slices = 0;

function getDocumentAsPDF() {

    Office.context.document.getFileAsync("pdf", { sliceSize: 2097152 }, function (result) {
        if (result.status == "succeeded") {
            // If the getFileAsync call succeeded, then
            // result.value will return a valid File Object.
            myFile = result.value;
            slices = myFile.sliceCount;
            document.getElementById("result").innerText = " File size:" + myFile.size + " #Slices: " + slices;

            // Iterate over the file slices.
            for (i = 0; i < slices; i++) {
                var slice = myFile.getSliceAsync(i, function (result) {
                    if (result.status == "succeeded") {
                        doSomethingWithChunk(result.value.data);
                        if (slices == i) // Means it's done traversing...
                        {
                            SendFileComplete();
                        }
                    }
                    else
                        document.getElementById("result").innerText = result.error.message;
                });
            }
            myFile.closeAsync();
        }
        else
            document.getElementById("result2").innerText = result.error.message;
    });
}

Upvotes: 5

Related Questions