Persisting data in word document using office add-in

877 views Asked by At

I'm trying to develop word add-in which allows to modify word content. One of the requirement is to select some text and mark it:

  • change its style (look) - and that is done
  • Associate that piece of text with some custom/hidden data, ex. an ID.

I want to somehow persist that data within the document, so that:

  • My add-in can read the document on the start and build a list of 'marked' elements
  • It should work on different machines - I can create a document on one machine, later open in on other machine, and add-in should be able to retrieve that data.

So far I tried to persist that data by first, getting data as HTML, and add attributes to tags, but that didn't work. Also tried to add some hidden div, but it was only available during word app lifetime. Recently, found this link: https://github.com/OfficeDev/Excel-Add-in-JavaScript-PersistCustomSettings , but still it allows to persist data in scope of "browser", so, I assume that on other machine it won't work.

What are my possibilities? Maybe I shouldn't store it within a document, but if not, where?

Developing it using office-js - it should work for both Word 2013 and Word 2016 (so I don't have an access to Word 2016 specific API).

2

There are 2 answers

1
Nick Carruthers On BEST ANSWER

Check out https://learn.microsoft.com/en-us/javascript/api/office/office.settings

The Office JS Settings object allows you to save custom data that persists with the document. It is pretty straight-forward to use. Getting settings is synchronous - saving settings is async. The biggest thing is that you have to remember to call Office.settings.saveAsync() after you have made your changes using Office.settings.set() in order to actually save the changes to the document.

0
dodocs On

Moreover to Nick's answer which is absolutely correct, you can use "Binding" mechanism. Bindings are stored in document out-of-the-box, so that, you can access those later at any time. An example, which fits my needs:

  • Select some text and click on button in add-in (to call some method)
  • Call addFromSelectionAsync() method (you can specify an id for it to reference it later)
  • Access it using getAllAsync() or getByIdAsync() method at any time you need it.

More about it: https://dev.office.com/docs/add-ins/develop/bind-to-regions-in-a-document-or-spreadsheet