1

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();
    });
  };
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
JesseJames
  • 13
  • 3
  • While everyone says the browser doesn't have access to the file system, you can in fact grant access, it just requires user input which might be OK based on your "user click button" statement. Is that OK? I have a function in my Add-In for listing files in a directory and can confirm it works as expected, it opens file explorer, user selects folder and it lists the files. I also have uploaded PDF/CSV etc and parsed into JSON. – FreeSoftwareServers Jun 22 '23 at 22:54
  • I haven't tested this functionality cross platform though, but should be OS agnostic, but I'd recommend testing. – FreeSoftwareServers Jun 22 '23 at 22:57
  • Thanks @FreeSoftwareServers thats one of the functionalities i need it to exactly do, im assuming to open a file explorer that would require an external module to allow that to happen or is something already capable from within the Microsoft JS API – JesseJames Jun 27 '23 at 23:52
  • I feel like you're struggling to seperate the "Microsoft JS API" from the idea that Add-Ins are just WebSites. You should be able to open a MS Word File and convert to `Base64` without anything to do w/ Office-JS and I would recommend testing outside of your Add-In. Then use the Office-JS API to inspect the base64 or do what you will. See this answer for opening files via JS --> https://stackoverflow.com/a/26298948/5079799 – FreeSoftwareServers Jun 28 '23 at 17:35

2 Answers2

1

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.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32
  • You can have a button to access the file system with user interaction. I have uploaded and parsed csv and pdf from local file system using Office-JS api and different frameworks for parsing files based on file type. – FreeSoftwareServers Jun 22 '23 at 22:56
0

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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45