View Single Post
 
Old 11-20-2012, 02:59 PM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

You don't really have to have a Node named for every CC title you want to map, you just need a node (see above.) If you do want a node name to match the CC name then:

HTML Code:
Sub MapCCs()
Dim oCC As Word.ContentControl
Dim oCustomPart As Office.CustomXMLPart
Dim oDoc As Word.Document
Dim lngIndex As Long
Dim oNode As CustomXMLNode
  Set oDoc = ActiveDocument
  ClearXMLParts
  Set oCustomPart = oDoc.CustomXMLParts.Add("<?xml version='1.0' encoding='utf-8'?><Root_Node></Root_Node>")
  For lngIndex = 1 To oDoc.ContentControls.Count
    Set oCC = oDoc.ContentControls(lngIndex)
    Select Case oCC.Title
      Case "Name"
        Set oNode = oCustomPart.SelectSingleNode("/Name")
        If oNode Is Nothing Then
          oCustomPart.AddNode oCustomPart.SelectSingleNode("/Root_Node"), "Name"
        End If
        
        oCC.XMLMapping.SetMapping "/Root_Node/Name[1]", , oCustomPart
      Case "Address"
        Set oNode = oCustomPart.SelectSingleNode("/Address")
        If oNode Is Nothing Then
          oCustomPart.AddNode oCustomPart.SelectSingleNode("/Root_Node"), "Address"
        End If
       
        oCC.XMLMapping.SetMapping "/Root_Node/Address[1]", , oCustomPart
      Case "Age"
        Set oNode = oCustomPart.SelectSingleNode("/Age")
        If oNode Is Nothing Then
          oCustomPart.AddNode oCustomPart.SelectSingleNode("/Root_Node"), "Age"
        End If
        oCC.XMLMapping.SetMapping "/Root_Node/Age[1]", , oCustomPart
    End Select
  Next lngIndex
End Sub
'Run ClearXMLParts prior to testing these examples.
Sub ClearXMLParts()
Dim lngIndex As Long
'MsgBox ActiveDocument.CustomXMLParts.Count
For lngIndex = ActiveDocument.CustomXMLParts.Count To 4 Step -1
  ActiveDocument.CustomXMLParts(lngIndex).Delete
Next lngIndex
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote