dodocs
dodocs

Reputation: 61

Persisting data in word document using office add-in

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:

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

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).

Upvotes: 4

Views: 1003

Answers (2)

Nick Carruthers
Nick Carruthers

Reputation: 94

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.

Upvotes: 4

dodocs
dodocs

Reputation: 61

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

Upvotes: 0

Related Questions