我有几个cc复选框需要在关联的富文本字段中插入特定/相关的quickpart ...但它不起作用!我只是试图让一个复选框(“AQA_Yes”)工作,然后将添加其他复选框。选中一个复选框,应将快速部分“Recall”插入到富文本字段“CC_All”中。如果取消选中该复选框,则应删除quickpart并将富文本字段设置为宽度为0.第一个错误发生在“Select Case ContentControl.Title”行 - 运行时错误'424' : 所需对象。下一个问题是无法违反“objBB.Insert Selection.Range”行。我需要修改该行,以便将Recall quickpart插入到CC_All富文本字段中;但我不确定这里的代码。
Sub CheckBox_Click()
Select Case ContentControl.Title
Case "AQA_Yes"
recallObj = "Recall"
RichText = True
If ContentControl.Checked Then
InsertExistingBuildingBlock ("Recall")
End If
End Select
End Sub
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Call CheckBox_Click
End Sub
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) _
.Categories("General").BuildingBlocks(BuildingBlockTitle)
' Insert the building block into the document replacing any selected text.
objBB.Insert Selection.Range
End Sub
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, _
Cancel As Boolean)
Call CheckBox_Click
End Sub
答案 0 :(得分:1)
对于第一个问题,您没有将ContentControl对象传递给过程Checkbox_Click,因此VBA不知道它是什么。试试这个:
'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
对于第二个,您需要获取“目标”的范围:RichText内容控件内的位置。问题是,你还没有真正的内容控制对象。我相信这是你想要的,但你错过了几件事:
Dim cc As ContentControl
'Doesn't work as it stands
Set cc = ThisDocument.SelectContentControlsByTag("CC_All")
除非你知道你需要ThisDocument(你没有),否则总是使用ActiveDocument,而不是ThisDocument
SelectContentControlsByTag不返回单个内容控件。它返回一组内容控件,因为多个内容控件可以具有相同的标题和/或标记。如果您确定只有一个,或想要第一个,那么您可以将该索引指定为代码行的一部分。
所以类似下面的内容应该有效:
Set cc = ActiveDocument.SelectContentControlsByTitle("Test")(1)
Dim rngCC as Word.Range
Set rngCC = cc.Range
objBB.Insert rngCC