View Single Post
 
Old 11-19-2012, 02:55 PM
RobsterCraw RobsterCraw is offline Windows 7 32bit Office 2010 32bit
Novice
 
Join Date: Nov 2012
Posts: 11
RobsterCraw is on a distinguished road
Default

Thanks gmaxey
I took your code and tried to modify it to find the title of a CC whose content matches the search text first and then use that title instead of master, but I am getting an error on the line marked with an *.

HTML Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Dim oCC As ContentControl
Dim i As Long
Dim oCCTitle As String
Set oRng = ActiveDocument.Range
  With oRng.Find
    'Finds the title of the existing CC with matching text
    .Text = "Text to find"
    While .Execute
        For i = 1 To ActiveDocument.ContentControls.Count
            If ActiveDocument.ContentControls(i).Range.Text = "Text to find" Then
            'object required error on following line
*            Set oCCTitle = ActiveDocument.ContentControls(i).Title
            End If
        Next i
  End With
  With oRng.Find
    'Assumes that you already have a mapped CC in the document titled "Master"
    .Text = "Text to find"
    While .Execute
      If Not oRng.InRange(ActiveDocument.SelectContentControlsByTitle(oCCTitle).Item(1).Range) Then
      Set oCC = ActiveDocument.ContentControls.Add(wdContentControlText, oRng)
      oCC.XMLMapping.SetMapping (ActiveDocument.SelectContentControlsByTitle(oCCTitle).Item(1).XMLMapping.xPath)
      oRng.Collapse wdCollapseEnd
      End If
    Wend
  End With
End Sub
Your updated code seems to produce an error
Reply With Quote