Word

Insert and Use Mapped Content Controls

Ease of Use

Intermediate

Version tested with

2007, 2010 

Submitted by:

gmaxey

Description:

Inserts a mapped ContentContol at the selection. Mapped content controls are bound to data stored in a document CustomXMLPart. 

Discussion:

Word users are frequently looking for a reliable and efficient method to enter data at one point in a document and have that data automatically repeated (or duplicated) at other locations in the document. In Word versions previous to to Word2007 this objective was met with bookmarks and reference fields or document variables and DocVariable fields. Both methods work well but their construction can be cumbersome and time consuming. Content Controls and more importantly their ability to be bound to data in a document CustomXMLPart introduced in Word2007 makes this process a walk in the sun. A CustomXMLPart is an information data store that users can create that becomes a part of the Word 2007/2010 OpenOfficeXMLFile format. When a content control is mapped (or bound) to data in this data store then whatever information is in the data store is displayed in the content control. It gets even better. Data entered in a content control updates the data in the data store. The beauty of this relationship is that you can copy and paste a mapped content control to one or many other locations in the document. If you change the data in one content control that change updated in the data store and reflected in all other copies of that content control. For whatever reason Microsoft has not made this powerful feature fully available to users through the user interface. The code provided here addresses that short sightedness and gives you a quick and simple method to create and use mapped plain text content controls in your documents. 

Code:

instructions for use

			

Option Explicit Dim oCustPart As Office.CustomXMLPart Sub AddContentControlAndMapToCustomXMLPart() Dim oRng As Word.Range Dim oCC As Word.ContentControl Dim xPath As String Dim pTitle As String Dim pNodeBaseName As String Set oRng = Selection.Range On Error GoTo Err_Handler Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng) pTitle = InputBox("Type the title this ContentControl", "Create Title") pNodeBaseName = Replace(pTitle, " ", "_") 'Node BaseNames can not contain spaces CreateDataNode pNodeBaseName xPath = "/Data/" & pNodeBaseName With oCC .Title = pTitle .XMLMapping.SetMapping xPath End With Set oRng = Nothing Set oCC = Nothing Exit Sub Err_Handler: If Err.Number = 4605 Then MsgBox "A content control already exists at the selected range. Please " _ & " select another location.", vbInformation + vbOKOnly, "Select Another Location" End If End Sub Sub CreateCustomPart() 'Establish the base CustomXMLPart. Set oCustPart = ActiveDocument.CustomXMLParts.Add _ ("<?xml version='1.0' encoding='utf-8'?><Data></Data>") ActiveDocument.Variables("custPartID").Value = oCustPart.ID Set oCustPart = Nothing End Sub Sub CreateDataNode(ByRef pBaseName As String) Dim oNode As CustomXMLNode Set oCustPart = ActiveDocument.CustomXMLParts.SelectByID _ (ActiveDocument.Variables("custPartID").Value) 'Create a child node for the content control bound data. If Not oCustPart Is Nothing Then Set oNode = oCustPart.SelectSingleNode("/Data") oCustPart.AddNode Parent:=oNode, Name:=pBaseName, NodeValue:="" Set oCustPart = Nothing Set oNode = Nothing Else 'The base CustomXMLPart does not yet exist. Create it. CreateCustomPart CreateDataNode pBaseName End If End Sub Sub CleanUp() 'Can be used to delete the CustomXMLPart used for mapping. On Error Resume Next Set oCustPart = ActiveDocument.CustomXMLParts.SelectByID _ (ActiveDocument.Variables("custPartID").Value) oCustPart.Delete On Error GoTo 0 End Sub

How to use:

  1. Copy above code.
  2. In Word press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save the document before any other changes.
 

Test the code:

  1. In Word click the Developer Tab>Code>Macros
  2. In the Macros Dialog, Macros In dropdown select your document
  3. Select the AddContentControlAndMapToCustomXMLPart procedure and click "Run"
  4. Enter a Title in the dialog that appears and click "OK"
  5. Select and copy the content control just created.
  6. Paste the content control to a new location.
  7. Enter text in either control and exit the control.
  8. Note how both controls display the entered text.
  9. If you use the attached sample file there is a button on the QAT you can use to demo the code.
 

Sample File:

KBArticle submission (mapped CCs).zip 21.19KB 

Approved by Jacob Hilderbrand


This entry has been viewed 197 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express