How to open new Word docx document in word Add-in
Asked Answered
D

3

6

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.

Dempstor answered 7/10, 2016 at 9:14 Comment(0)
G
11

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


    }
Guttersnipe answered 7/10, 2016 at 21:28 Comment(9)
Thanks, it is working as expected, waiting for the production.Dempstor
is it still not in production? There is no application attribute in context in the official docs 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.Helenhelena
is it somehow possible to get available functions for the application-object?Helenhelena
As of now the application object only has this single method (in our preview )Guttersnipe
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.Dominique
@JuanBalmori We are getting GeneralException when calling context.sync() after calling myNewDoc.Open(). OfficeJS API is referred from this CDN URL - appsforoffice.microsoft.com/lib/1/hosted/Office.jsVariegation
@ChiragVidani can you share your Office Build?Guttersnipe
@JuanBalmori Its MS Word Version: 1803 (Build 9126.2336 Click-to-run)Variegation
@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?Metric
B
1

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.

Batsman answered 7/10, 2016 at 11:14 Comment(2)
@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?Batsman
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 github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_Openspec/… here more info on requirements to try it github.com/OfficeDev/office-js-docs/tree/WordJs_1.3_OpenspecGuttersnipe
T
0

Following on years after the question, today we have a pretty straitforward way to doing this in word javascript API. Just have to use the methods from Word.Application class.

For your described use case, you could work out a way to use either

  • .createDocument(base64File: string): to create Word.CreatedDocument class object, which resembles Word.Document class.
  • openDocument(filePath: string): to open a new Word tab or window with the file from the path (which may be a local path or even a url, depending on your environment use case.

Just for the record, the other methods of the Word.Application class are amazing and could be very handy, like retrieveStylesFromBase64(base64File)

Teodor answered 11/12, 2023 at 6:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.