Search code examples
vbams-wordopenxml

Unable to extract and re-insert MS Word Content Control using VBA and InsertXML


This question is related to my other question: Range.InsertXML using Transform

In MS Word it is easy to insert a content control using VBA, for example:

ThisDocument.ContentControls.Add wdContentControlRichText, Selection.Range

I've recently started exploring more in the XML side of things, e.g.: Debug.Print ThisDocument.Range.XML seems to (or actually does) produce the XML for a Word document. However, if I create a NEW, BLANK document and add a Content Control I am unable to extract and reinsert the Content Control (oCC).

My steps:

  • added 2 blank paragraphs to a new document
  • added oCC to the 2nd paragraph
  • selected the oCC paragraph
  • immediate window: thisdocument.Paragraphs(1).Range.InsertXML selection.Range.XML

At first glance it LOOKS like the Content Control was duplicated, BUT on closer inspection, it was deleted and only the formatted text remains (see image, top paragraph is actually just formatted text).

Failed attempt to add content control using InsertXML

Thinking I could out smart MS Word I set the properties of the Content Control to '...can not be deleted', but that didn't help.

I've also tried to insert into a separate document in case the issue had something to do with duplication of something that ought to have been unique.

In a nutshell:

To answer this question I need a way to insert a Content Control to a document using a combination of VBA and XML (or confirmation that what I am attempting is not possible).


Solution

  • Just realized I should use Selection.Range.WordOpenXML instead of Selection.Range.XML