1

使用 VB for Excel,但对 VB for Word 来说是新的。如果某个复选框被标记为 true,我不确定如何扩展标题。这是我目前拥有的代码,我得到一个运行时错误,说集合的请求成员不存在,但我在控件的属性窗口中命名了 CheckBox。我正在使用 Microsoft Word 版本 1808(内部版本 10730.20262 即点即用)。

Sub Macro1()

If ActiveDocument.FormFields("Licensing_1").CheckBox.Value = True Then
    Do Until Selection.Find.Found = False
        If Selection.Text Like "Licensing Discovery Questions" Then
        Selection.Find.Style = ActiveDocument.Styles("Heading 1")
        Selection.Find.Execute
            Else: Selection.Paragraphs(1).CollapsedState = True
        Selection.Find.Style = ActiveDocument.Styles("Heading 1")
        Selection.Find.Execute
    End If
Loop
End If

End Sub
4

1 回答 1

0

与表单域不同,多个 Word 内容控件可以具有相同的名称(名称 - 实际术语是Title)。出于这个原因,不能将字符串用作索引值——不能保证它是唯一的。

出于这个原因,有必要使用方法SelectContentControlsByTitle(或SelectContentControlsByTag)来获取内容控件对象。该方法返回内容控件的集合——所有控件都具有相同的标题或标签。(注意:这些是区分大小写的!)。

如果代码应与所有这些内容控件一起使用,则For Each对集合使用循环。

如果代码只适用于一个内容控件(例如第一个),则可以使用该ContentControls.Item属性来指定。

假设文档中只有一个复选框的名称为“Licensing_1”:

Sub CheckBox_CC()
    Dim ccCheckBox As Word.ContentControl
    Dim doc As Word.Document

    Set doc = ActiveDocument
    Set ccCheckBox = doc.SelectContentControlsByTitle("Licensing_1").Item(1)

    If ccCheckBox.Checked Then
        'Do things here
    End If
End Sub
于 2018-12-21T19:02:22.330 回答