vba 在 Word 2010 中修改文本框的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9224615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:12:10  来源:igfitidea点击:

Checkbox to modify textbox in Word 2010

vbams-wordword-vba

提问by Nowacki

I want to have a MS Word 2010 document where there is a checkbox (ckeckable) and a textbox (textfield) where different text is displayed depending on whether the checkbox is clicked or not.

我想要一个 MS Word 2010 文档,其中有一个复选框(可检查)和一个文本框(文本字段),其中根据是否单击复选框显示不同的文本。

I have tried searching for it but somehow all the suggestions are not meant as solutions for the checkbox question...

我试过搜索它,但不知何故所有的建议并不意味着作为复选框问题的解决方案......

I would think that the solution should be used in Visual Basic?

我认为该解决方案应该在 Visual Basic 中使用吗?

回答by Jean-Fran?ois Corbett

Something like this?

像这样的东西?

Private Sub CheckBox1_Change()
    If CheckBox1.Value = True Then
        TextBox1.Text = "Checked!"
    Else
        TextBox1.Text = "Unchecked."
    End If
End Sub

This assumes you have a checkbox called CheckBox1and a text box called TextBox1. The above code goes in the ThisDocument module.

这假设您有一个名为的复选框CheckBox1和一个名为的文本框TextBox1。上面的代码在 ThisDocument 模块中。

Result looks like this enter image description hereand this enter image description here.

结果看起来像这样在此处输入图片说明和这样在此处输入图片说明

EDITWhoops, I made these pics in Excel... Oh well, they look almost identical in Word.

编辑哎呀,我用 Excel 制作了这些照片......哦,它们在 Word 中看起来几乎相同。

EDITYou have now changed the requirement and want the textbox to "be hidden" when the checkbox is unchecked. There is no formal way to "hide" a textbox, but you can remove it visible features, i.e. the text it contains as well as the "sunken" special effect, such that it is indistinguishable from its background:

编辑您现在已经更改了要求,并希望在取消选中复选框时“隐藏”文本框。没有正式的方法来“隐藏”文本框,但您可以删除它的可见特征,即它包含的文本以及“凹陷”特殊效果,使其与背景无法区分:

Private Sub CheckBox1_Change()
    If CheckBox1.Value = True Then
        TextBox1.Text = "Checked!"
        TextBox1.SpecialEffect = fmSpecialEffectSunken
    Else
        TextBox1.Text = ""
        TextBox1.SpecialEffect = fmSpecialEffectFlat
        'Textbox is now "invisible"
    End If
End Sub