none
How can I map word content with custom identifier? RRS feed

  • Question

  • Hi,

    I want to map MS word content with custom identifier e.g.

    suppose word contains below text.

    My contract value is 1000 and is applicable for country US.

    So here I want to map 1000 & US with respective identifier, so that we can know in case of change in those any point of time.

    I know content control can be used but I am looking if there is any other approach available. 

    Thursday, August 8, 2019 9:18 AM

All replies

  • If you can be more precise about what you need it would be easier to answer. But for example,

    If you want Word to fire an event of some kind as you actually change the text that you have marked, the only kind of marker that lets you do that is an ActiveX text field. Then you have a named field that VBA recognises as an object with a Change event. Personally, I would advise against using this field type, and they will certainly only work on the Windows version of Word.

    Even with content controls, you do not actually get a change event of that kind. What you get are events that are fired when you navigate outside the control. You could also do that using a legacy (non-ActiveX) form field. These don't use the VBA Event mechanism, but you can specify a VBA macro that will run when you navigate away from the field (e.g. by tabbing to the next one).

    You can simulate a change event with content controls using the approach developed by Greg Maxey (I think it is at https://gregmaxey.com/word_tip_pages/validate_content_control_entries.html ) This also releies on techniques that are unlikely to work on Mac.

    If you are using VSTO, you can used its "enhanced Bookmark" to fire events that would let you detect a change.

    If instead you just want to mark some text in some with a unique identifier, you could use any of the above approaches but all you really need is to select the text and insert a Word bookmark. Alternatively (and a lot messier) you could mark text with a unique character style name.

    If you want to be able to replace text in named locations, then the options include all of the above really, plus you could use either legacy Custom Document Properties with DOCPROPERTY fields, Document Variables with DOCVARIABLE fields. 

    If you are actually defining a mailmerge, you should be using MERGEFIELD and related fields.


    Peter Jamieson

    Saturday, August 10, 2019 3:57 PM
  • Hi Peter,

    I am trying to parse document (mostly using Open XML) and storing metadata (e.g. previously given information - contract value/country) to our application storage.

    As of now I have evaluated content controls (as suggested by you) and seems to be a good option, but some time when I try to edit data within content controls, it is possible to delete the block mistakenly (in case of select all text and hit back-space), so in that case it would not be possible for me to read metadata. Also I cannot enable property "Cannot delete content control" as end user is actually trying to get rid of that metadata from the document.

    Additionally, I need possible approaches to work on both MS word for windows & mac.

    Sunday, August 18, 2019 5:03 AM
  • The basic problem with using either bookmarks or special style names to mark areas is the fact that it is very easy for the user to delete the bookmark or the entire chunk of text with the style. So really they are no different in that respect, and they don't have any features to raise events if that's what you need.

    If you don't need the events, one thing you could consider would be to use the document protection feature to protect the entire document, but mark areas that you want to be editable. But before you do that, you create a bookmark for each editable area that includes at least one character in the surrounding area that you are going to protect.

    e.g. if you have text like 

    My contract value is 1000 and is applicable for country US.

    You bookmark the text " 1000 " and " US.", then protect the areas "My contract value is ", " and is applicable for country " and "."

    Then the user would not (AFAIK) be able to delete the bookmarks easily without unprotecting the document. To extract the values, you would read the text "covered" by the bookmarks and delete the first and last characters.

    You can set up such documents on Windows Word. The user sees a task pane that allows them to navigate from one editable area to another.

    Unfortunately, on Mac Word (365/2016/2019) the facilities to mark and navigate such areas are absent. Although experiments here suggest that a document created and protected on Windows Word will function as expected on Mac Word, it's probably not a good idea to rely on such a feature to work.

    However, that would obviously be pretty useless if what you really want is for people to be edit the entire document.

    So I think there is a bit of an impasse - content controls do a lot of what you need, but only really if you can protect them.

    I wonder what the overall process for your users looks like, and whether for example you could provide them with a function that extracted the metadata then converted all the relevant content controls to plain text? Or maybe that is something you could do after the user has completed the document, just modifying the XML e.g. using the Office Open XML API?




    Peter Jamieson

    Sunday, August 18, 2019 6:15 PM