context.application is undefined when using WordAp

2019-07-18 15:21发布

问题:

With WordApi 1.3 it should be possible to use the new createDocument method to create new documents from within the web addIns.

However, when using the example snippet below found in the documentation, context.application is undefined.

Word.run(function (context) {        // lets hold a valid base64 docx on this variable...        
    var myStartingDocAsBase64 = "some valid base64 encoded docx";
    var myNewDoc = context.application.createDocument(myStartingDocAsBase64); // note that the parameter is optional, a blank doc will be created otherwise               // at this point you can use the entire API on the myNewDoc document.. you can do things like        
    myNewDoc.body.insertParagraph("This is a new paragraph added via API", "end");        //now lets open the document, after this method is called,  you will no longer be able to modify the doc.....        
    myNewDoc.open();
    return context.sync();    
})
.catch(function (e) {
   console.log(e.message);
})

This is even though Office.context.requirements.isSetSupported("WordApiDesktop", "1.3") returns true.

This is tested in Office 1611 on Windows 10 (shown in the image below).

Are the features in 1.3 not fully implemented yet, or should we do something else to get createDocument to work?

回答1:

Hello thanks for your question. All of this is still in preview and hence subject to change. Here is some guide for you.

  1. 1.There is no such thing as the WordApiDesktop requirement set, this was removed and will not longer exist in Office.js quite soon. The fact that is there is a bug, so thanks for reporting this.
  2. If you see the documentation page you shared, the application object and its createDocument method are currently (and subject to change) in the 1.3 requirement who is currently in preview. (use the preview CDN of office.js https://appsforoffice.microsoft.com/lib/beta/hosted/office.js )

So please don't use the WordApiDesktop requirement, and also make sure you are using the BETA CDN for Office.js

its allways good to have the latest Office to test the previews, check out this page for help in case you are not in latest: https://github.com/OfficeDev/office-js-docs/blob/215f5d35490c943cc06c29b98357ba8cb034ec81/docs/develop/install-latest-office-version.md