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

限于喜欢 提交于 2020-06-10 02:04:38

问题


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.


回答1:


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


    }



回答2:


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.



来源:https://stackoverflow.com/questions/39913645/how-to-open-new-word-docx-document-in-word-add-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!