Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #8  
Old 11-20-2012, 07:49 AM
RobsterCraw RobsterCraw is offline Creating a plain text content control for every instance of a word or phrase Windows 7 32bit Creating a plain text content control for every instance of a word or phrase Office 2010 32bit
Novice
Creating a plain text content control for every instance of a word or phrase
 
Join Date: Nov 2012
Posts: 11
RobsterCraw is on a distinguished road
Default

Ok, I took what gmaxey provided and them I added the InputBox for the query and then some other controls. It works, but when the function would create multiple CCs all mapped together (like if you were to query for text that is repeated several times in a document), the script returns an error, at the line below with all the **** in the line.

*** Set oCC = ActiveDocument.ContentControls.Add(wdContentContro lText, oRng)

"Run-time error '4198':
Command failed"

It still does what I want it to do, so if I can clear this error then it will be perfect.

HTML Code:
Sub CreateCCForMatchedText()
'Much thanks to Greg Maxey for doing the hard part
Dim oRng As Word.Range
Dim oQuery As String
Dim oCC As ContentControl
Dim i As Long
Dim oCCTitle As String
Dim oCCTitleAssignment As String
Dim oCancelOperation As Integer
Dim oSameTitle As Boolean

Set oRng = ActiveDocument.Range
oSameTitle = False
oQuery = InputBox("Enter text to bind to Content Controls", "Find and Bind")
  With oRng.Find
    .Text = oQuery
    While .Execute
        'Find the Title for any CC that whose contents match the query
        For i = 1 To ActiveDocument.ContentControls.Count
            If ActiveDocument.ContentControls(i).Range.Text = oQuery Then
            oCCTitle = ActiveDocument.ContentControls(i).Title
            End If
        Next i
        Do While oCCTitle = ""
        'Loop to ensure that each CC created will have a title if none exists
            oCCTitleAssignment = InputBox("You Must Enter the Title for the Content Controls to be added", "Enter Title", "")
            If oCCTitleAssignment = "" Then
                oCancelOperation = MsgBox("You didn't enter a title for this Content Control, do you want to cancel the operation?", vbYesNo, "error")
                If oCancelOperation = vbYes Then
                    Exit Sub
                End If
                'Make sure that the title isn't already taken by a different CC
                For i = 1 To ActiveDocument.ContentControls.Count
                    If ActiveDocument.ContentControls(i).Title = oCCTitle And ActiveDocument.ContentControls(i).Range.Text <> oQuery Then
                        oSameTitle = True
                    End If
                Next i
                If oSameTitle = True Then
                    MsgBox ("This Title is already used for another Content Control, Please Choose Another")
                End If
            End If
        Loop
        'Then map the CC to the customXMLpart and assign the title to the CC
        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)
        oCC.Title = oCCTitle
        oRng.Collapse wdCollapseEnd
        End If
    Wend
  End With
End Sub
Reply With Quote
 

Tags
content control, find & replace, xml



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a plain text content control for every instance of a word or phrase Rich text/Plain text Content Controls in Template michael.fisher5 Word 9 11-19-2014 06:36 AM
Creating a plain text content control for every instance of a word or phrase How do you set rich text in a content control Testor Word VBA 4 07-08-2012 07:55 AM
Word2010 check boxes and plain text content control boxes in same table fcsungard Word 5 06-01-2012 01:16 AM
Creating a plain text content control for every instance of a word or phrase Rich Text Content Control - Allow User Formatting keithacochrane Word 1 05-28-2012 05:06 PM
Templates: automatic text generation from Rich Text content control Chickenmunga Word 0 10-01-2008 11:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft