How to add AND get a customXMLPart programmatically

Viewed 657

I am trying to save a part of a Word document into a customxmlpart temporarily. E.g. I am implementing a "hide" feature, which is missing in the 2016 apis.

I will have user write those parts in a content control, and read the ooxml contents of it. This much I have done successfully.

Now I need to hide the ooxml inside the same document, to read it later.

I was thinking the functions in Office.CustomXmlParts, e.g. addAsync(), getByIdAsync(), and getByNamespaceAsync() should do the job, but there are problems:

  1. addAsync() does not seem to give you any id or namespace, so that you can get the xml later. Where do I get the id/namespace to give to get... methods?
  2. Also Office.CustomXmlParts does not list a constructor. How do I get one to begin with?

I am new to this, thanks.

1 Answers

Office objects don't have constructors. You create one by using an add* method on a parent collection object. In this case, the Office.context.document.customXmlParts.addAsync method. There is a callback to this method that receives an AsyncResult object from Office. This object has the id of the new CustomXmlPart in the AsyncResult.value.id property. Your code should save the ID in a document seting for later retrieval. Details and code samples about all of this are at How to save custom xml to the document and Document settings.

Related