How to open existing document with Office JS API

Miikka Laakso 41 Reputation points
2022-04-26T05:32:27.603+00:00

Is there a way to open existing document with Office JS API (Word, Excel, PowerPoint) as we can open new document with stream? If there is no API support, is there protocol URLs which we could use (in desktop/browser win/mac) to accomplish this?

Our use case is that we automate document saving process so that document is stored to predefined location. After this is done, we need to open this copy for user.

Microsoft 365 and Office Development Office JavaScript API
Microsoft 365 and Office Development Other
{count} votes

3 answers

Sort by: Most helpful
  1. Miikka Laakso 41 Reputation points
    2022-05-05T06:11:00.187+00:00

  2. Goetting, Dennis 0 Reputation points
    2023-01-29T21:34:45.4833333+00:00

    Hi,

    apparently there has been an update recently, which based on some code examples seen allows to open a document using the office JS API. See also the MIcrosoft documentation

    createDocument(base64File)

    Creates a new document by using an optional base64 encoded .docx file.

    TypeScriptCopy

    createDocument(base64File?: string): Word.DocumentCreated;
    
    
    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); });
    
    
        }
    createDocument
    

  3. peiye zhu 165 Reputation points
    2023-08-27T23:30:42.5366667+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.