5

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.

user3931619
  • 267
  • 4
  • 14

2 Answers2

10

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); });


    }
Juan Balmori
  • 4,898
  • 1
  • 8
  • 17
  • Thanks, it is working as expected, waiting for the production. – user3931619 Oct 08 '16 at 06:35
  • is it still not in production? There is no `application` attribute in context in the official docs https://dev.office.com/reference/add-ins/shared/context. Btw. I use angular 4 and there is no application-attribute on the RequestContext-Class in the types for Word-Namespace. – JohnnyAW Aug 10 '17 at 14:09
  • is it somehow possible to get available functions for the application-object? – JohnnyAW Aug 10 '17 at 14:12
  • As of now the application object only has this single method (in our preview ) – Juan Balmori Aug 10 '17 at 14:15
  • It does not work properly if the document contains macros. "Enable content" button appears in the first document (instead of the document being opened) but pressing it does not enable macros in the second document. – Sergey Belikov Jan 22 '18 at 15:24
  • @JuanBalmori We are getting GeneralException when calling context.sync() after calling myNewDoc.Open(). OfficeJS API is referred from this CDN URL - https://appsforoffice.microsoft.com/lib/1/hosted/Office.js – Chirag Vidani Jan 25 '19 at 10:16
  • @ChiragVidani can you share your Office Build? – Juan Balmori Feb 04 '19 at 17:59
  • @JuanBalmori Its MS Word Version: 1803 (Build 9126.2336 Click-to-run) – Chirag Vidani Feb 05 '19 at 12:33
  • @JuanBalmori - this code works as expected but the new document is created in compatibility mode, is there a work around to over come compatibility mode? – Bala P Dec 19 '19 at 17:03
0

Opening a document in a new instance (i.e. a new, separate Word window) is, at least for the time being, not supported by the JavaScript-based Office.js API. You are always starting from a Context object, which will give you access to the currently active document via the Context.document property.

What you can do is insert content into an existing document, e.g. via the body.insertOoxml method.

Currently, the Office.js API is still limited as compared to the classic COM API. If you need the full feature-set you still might consider developing a COM or VSTO solution today. The only downside is that your add-in won't run on any platform other than Windows desktop.

Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
  • @JuanBalmori: This is great news! But if I understand your answer correctly, this is still in beta. Is there any documentation on beta features available? Or any other way to follow up on the features that are going to be added? – Dirk Vollmar Oct 07 '16 at 21:37
  • yes its on preview! please give it a try! send me feedback! we have an open spec where we drop everything we are planning to ship! check it out https://github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec/word here more info on requirements to try it https://github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec – Juan Balmori Oct 07 '16 at 22:02