Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open new Word docx document in word Add-in

I have developed a word add-in using word javascript api. My Document .docx file is on server and i need to open that .docx document as a new word document on a button click in add-in.

Please guide me how i can open new document in word add-in.

Thanks.

like image 355
user3931619 Avatar asked Feb 06 '23 23:02

user3931619


1 Answers

There is a new method we are adding to the API that you can actually use to achieve this. Notice that is in preview, which means will be in production in a couple of months. You need latest Office version plus reference our preview office.js to try it. The office.js preview is here https://appsforoffice.microsoft.com/lib/beta/hosted/office.js

Check out this code sample on how easy is to do it.

 function onaddOpenDoc() {
        Word.run(function (context) {
          
          // this getDocumentAsBase64 assumes a valid base64-encoded docx file
            var myNewDoc = context.application.createDocument(getDocumentAsBase64());
            context.load(myNewDoc);

            return context.sync()
                .then(function () {
                    myNewDoc.open();
                    context.sync();
                }).catch(function (myError) {
                    //otherwise we handle the exception here!
                    showNotification("Error", myError.message);
                })

        }).catch(function (myError) { showNotification("Error", myError.message); });


    }
like image 158
Juan Balmori Avatar answered Feb 08 '23 15:02

Juan Balmori