JesseJames
JesseJames

Reputation: 13

Open a .docx file on my computer using Microsoft JavaScript API

I'm creating a Microsoft word addin using typescript, react and the word API.

One of the features of this add-in is the ability for the user to be able to open a document located on their computer for the example lets go with "C:\Test\Test.docx" when they click a button thats located in the taskpane of the addin.

using the words api I feel its possible to open an existing document if i can first convert that document or filepath (im not sure) to a base64 string and use it with the method "createDocument(base64File)" More info

However, im not sure how to get that base64 string everything I've googled seems to lead me to have to upload my document and it returns a base64 string. Further I'm not sure if that's exactly what needs to be done to open an already existing document.

I haven't got any code of value to show.

const openWordDocument = (): void => {
    Word.run(async (context) => {
      context.application.createDocument().open();
    });
  };

Upvotes: 1

Views: 1006

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

Like Rick has already mentioned, Office web add-ins, as well as any other web applications, don't have access to the file system. They are run in a sandbox environment (browser) without direct access to the file system. The best what you could do is to save the file to the local storage. So, the aim of the createDocument(base64File) method is to be able to download the file and then open it in Word.

You can post or vote for an existing feature request on Tech Community where they are considered when the Office dev team goes through the planning process.

Upvotes: 0

Rick Kirkham
Rick Kirkham

Reputation: 9659

An Office add-in is essentially a web application and it has the same restrictions as a web application. It doesn't have access to the computer's file system (except for special tasks like saving cookies). So it cannot open a file on the computer. Your add-in can get a file stored online, such as OneDrive or SharePoint, convert it to base64, and then pass it to the createDocument method.

Upvotes: 1

Related Questions