View Single Post
 
Old 11-20-2012, 10:13 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

I'm having a hard time keeping up with you. It seems that you are trying to do a couple of things inside the With.Find ... While .Execute.

Seems to me that you should deal with existing CCs first then do the find and deal with plain text that you want to create a CC and map.

HTML Code:
Sub AddContentControlAndMapToLocalXML()
Dim oCC As Word.ContentControl
Dim oCustomPart As Office.CustomXMLPart
Dim xmlPart As String
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim strQuery As String
Dim lngIndex As Long
Dim strCC_Title As String
Dim bTitled As Boolean
  Set oDoc = ActiveDocument
  bTitled = False
  ClearXMLParts
  xmlPart = "<?xml version='1.0' encoding='utf-8'?><Root><Item></Item></Root>"
  Set oCustomPart = oDoc.CustomXMLParts.Add(xmlPart)
  strQuery = InputBox("Enter text to bind to Content Controls", "Find and Bind")
  'Deal with existing CCs
  'Find the title for any existing CC who's contents match the querry.
  For lngIndex = 1 To oDoc.ContentControls.Count
    Set oCC = oDoc.ContentControls(lngIndex)
    If oCC.Range.Text = strQuery Then
      'Define title.
      If Not bTitled Then
        'Is CC titled?
        strCC_Title = ActiveDocument.ContentControls(lngIndex).Title
        If Len(strCC_Title) > 0 Then
          bTitled = True
        Else
          Do
            strCC_Title = InputBox("Enter a CC title")
            If StrPtr(strCC_Title) = 0 Then GoTo lbl_Exit 'User canceled
          Loop Until strCC_Title <> ""
          oCC.Title = strCC_Title
        End If
      End If
      'Is the CC mapped?
      If Not oCC.XMLMapping.IsMapped Then
        'Map it.
        oCC.XMLMapping.SetMapping "/Root/Item[1]", , oCustomPart
      End If
    End If
  Next lngIndex
  'Find text, add and map CCs.
  Set oRng = ActiveDocument.Range
  If Len(strCC_Title) = 0 Then
    Do
      strCC_Title = InputBox("Enter a CC title")
      If StrPtr(strCC_Title) = 0 Then GoTo lbl_Exit 'User canceled
    Loop Until strCC_Title <> ""
  End If
  With oRng.Find
    .Text = strQuery
    .Wrap = wdFindStop
    While .Execute
      Set oCC = oDoc.ContentControls.Add(wdContentControlText, oRng)
      oCC.XMLMapping.SetMapping "/Root/Item[1]", , oCustomPart
      oCC.Range.Text = strQuery
      oCC.Title = strCC_Title
      oRng.Collapse wdCollapseEnd
      Do While oRng.InRange(oCC.Range)
        oRng.Move wdCharacter, 1
      Loop
    Wend
  End With
lbl_Exit:
  Exit Sub
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