简体   繁体   English

如何使用UserForm CheckBox更改Word文档中的Control Content CheckBox?

[英]How to use UserForm CheckBox to change Control Content CheckBox in Word Document?

I created an UserForm with a checkbox 'cbxYes' and a Content Control checkbox 'docCbx' in the Word document. 我创建了一个复选框,一个用户窗体 'cbxYes'和内容控制复选框'docCbx'话语文件内。 I want to be to check off the checkbox 'cbxYes' in UserForm that then changes the Content Control checkbox in the Word document. 我想在用户窗体中选中'cbxYes'复选框,然后更改Word文档中的“内容控制”复选框。 So the input is from the UserForm checkbox and the output is the Content Control checkbox. 因此,输入来自UserForm复选框,输出是Content Control复选框。

I have tried multiple searches on how to do this, but I could not find exactly what I needed. 我已经尝试过多次搜索以了解如何执行此操作,但是我找不到确切的需求。 Most of the searches were related to Excel. 大多数搜索与Excel相关。 And honestly, I don't know what I'm doing. 老实说,我不知道我在做什么。 Please. 请。 The correct help is greatly appreciated. 正确的帮助将不胜感激。

Private Sub cbxYes_Click()

Dim oCC As ContentControl

If cbxYes.value = True Then
   cbxYes.value = "True"
   ActiveDocument.docCbx_Yes.value = True
Else
   cbxYes.value = "False"
   ActiveDocument.docCbx_Yes.value = False
End If

End Sub

The error I got was: 我得到的错误是:

run-time error '438': Object doesn't support this property or method. 运行时错误“ 438”:对象不支持此属性或方法。

Assuming that "docCbx" is the title of the content control you can replace 假设“ docCbx”是内容控件的标题 ,则可以替换

ActiveDocument.docCbx_Yes.value = True

by 通过

For Each oCC In  ActiveDocument.SelectContentControlsByTitle("docCbx")
  If (oCC.Type = wdContentControlCheckBox) Then 
    oCC.Checked = False
  End If
Next

and the equivalent, but using False, for the other branch of your code. 和等价的代码,但对其他代码分支使用False。 The above code will update all checkbox type content controls with that name (the names do not have to be unique within a document), except controls that have been marked as "Contents cannot be edited" - they will remain unchanged. 上面的代码将使用该名称更新所有复选框类型的内容控件(名称在文档中不必唯一),但标记为“内容无法编辑”的控件除外-它们将保持不变。 It deals with the situation where there aren't any content controls with that name by doing nothing . 它处理了什么都不做的情况,其中没有任何内容控件具有该名称。

If "docCbx" is the value of the Tag , you would need the following instead: 如果“ docCbx”是Tag的值,则需要以下内容:

For Each oCC In  ActiveDocument.SelectContentControlsByTag("docCbx")
  If (oCC.Type = wdContentControlCheckBox) Then 
    oCC.Checked = False
  End If
Next

If docCbx is something else, I would suggest that instead you give Titles and/or Tags to your content controls and use the above approach. 如果docCbx是其他内容,我建议您改为为内容控件提供标题和/或标签,并使用上述方法。 Otherwise, you should modify your question to state precisely how you are naming the control "docCbx". 否则,您应该修改问题,以准确说明控件的名称“ docCbx”。

You get the error because adding content controls to a document does not create new members of the document object, whereas adding a form control to a user form does create new members of the Form object. 您收到错误消息是因为向文档中添加内容控件不会创建文档对象的新成员,而向用户表单中添加表单控件却会创建Form对象的新成员。 (Documents do in fact have a mechanism that works more like the Form object but it has been deprecated for a long time.) (事实上​​,文档确实具有一种机制,其工作方式与Form对象类似,但已被弃用了很长时间。)

The code shown in the question would be for ActiveX checkboxes, rather than content controls. 问题中显示的代码将用于ActiveX复选框,而不是内容控件。 (Just to make things really complicated, Word also has checkbox form fields that need yet another code syntax.) (只是使事情变得非常复杂,Word还具有复选框表单字段 ,需要其他代码语法。)

There's no way to refer directly to a content control name via the Document object - it must be done over the ContentControls collection. 无法通过Document对象直接引用内容控件名称-必须在ContentControls集合上完成。 Content controls can be assigned a Title and/or a Tag in the Properties dialog box. 可以在“属性”对话框中为内容控件分配Title和/或Tag

在此处输入图片说明

More than one content control can have the same name or title, which makes the code a bit complicated. 多个内容控件可以具有相同的名称或标题,这使代码有点复杂。 Querying Document.ContentControls("Title") returns a collection (all the content controls with that title). 查询Document.ContentControls("Title")返回一个集合(所有带有该标题的内容控件)。

If you know which one, then it's possible to pick it up directly (rather than working through a collection) using the Item method, specifying that it's the n content control (the index in the order of content controls). 如果您知道哪一个,则可以使用Item方法直接指定它(而不是通过一个集合),并指定它为n个内容控件(内容控件顺序中的索引)。 This is usually used when one knows there's only the one. 通常在只有一个人知道的情况下使用。

Note, please, also, that in order to "check" or "uncheck" a content control checkbox the Checked property should be use. 另请注意,为了“选中”或“取消选中”内容控件复选框,应使用Checked属性。 So: 所以:

Private Sub cbxYes_Click()

Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("docCbx").Item(1)

If cbxYes.value = True Then
   'cbxYes.value = "True" not needed
   occ.Checked = True
Else
   'cbxYes.value = "False" not needed
   oCC.Checked = False
End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM