I have several cc checkboxes that need to insert a specific / relevant quickpart into an associated rich text field... but it's not working!!! I'm just trying to get one checkbox ("AQA_Yes") working and then will add the others. One the check box is selected, it should be inserting the quick part "Recall" into the rich text field "CC_All". If the check box is de-selected, then the quickpart should be removed and the rich text field set to a width of 0. The first error occurs on the "Select Case ContentControl.Title" line -- Run-Time Error '424': Object Required. The next problem is trouble defying the "objBB.Insert Selection.Range" line. I need to modify the line so that it inserts the Recall quickpart into the CC_All rich text field; but I'm not certain of the code here.
Select Case ContentControl.Title
recallObj = "Recall"
RichText = True
If ContentControl.Checked Then
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Sub InsertExistingBuildingBlock(BuildingBlockTitle As String)
Dim objTemplate As Template
Dim objBB As BuildingBlock
Dim cc As ContentControl
Set cc = ThisDocument.SelectContentControlsByTag("CC_All")
Set ccObj = Selection
' Set the template to store the building block
Set objTemplate = ActiveDocument.AttachedTemplate
' Access the building block through the type and category
Set objBB = objTemplate.BuildingBlockTypes(wdTypeQuickParts) _
' Insert the building block into the document replacing any selected text.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, _
Cancel As Boolean)
For the first problem, you aren't passing the ContentControl object to the procedure Checkbox_Click, so VBA doesn't know what it is. Try this:
'Note: Also for OnExit! Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl) Call CheckBox_Click(ContentControl) Sub CheckBox_Click(ContentControl as Word.ContentControl) Select Case ContentControl.Title
For the second, you need to get the Range of the "target": a location inside the RichText content control. The problem is, you don't really have a content control object, yet. I believe this is the one you want, but you're missing a couple of things:
Dim cc As ContentControl 'Doesn't work as it stands Set cc = ThisDocument.SelectContentControlsByTag("CC_All")
ALWAYS use ActiveDocument, not ThisDocument unless you know you need ThisDocument (you don't)
SelectContentControlsByTag does not return a single content control. It returns an array of content controls because more than one content control can have the same title and/or tag. If you're sure you have only the one, or want the first, then you can specify that index as part of the line of code.
So something like the following should work:
Set cc = ActiveDocument.SelectContentControlsByTitle("Test")(1) Dim rngCC as Word.Range Set rngCC = cc.Range objBB.Insert rngCC