如何针对ActiveX标签内联猿来更改背景颜色并隐藏标签中的文本



我刚刚从一个使用ActiveX文本框控件的网站上找到了这段代码。我希望它能在标签ActiveX控件上工作,当我提交命令按钮控件时,标签中会发生两件事:标签从红色变为白色。(我的目标是隐藏标签(。

我正在尝试的代码可以使用文本框控件,但不能使用标签,因为我认为它们是不同的数据类型?(我得到一个不匹配的错误(。你能帮忙吗?谢谢你?

以下是我尝试过的,但出现错误:

Dim ils As Word.InlineShape
Dim olef As Word.OLEFormat
Dim tb As MSForms.Label
Set ils = ActiveDocument.InlineShapes(1)
Set olef = ils.OLEFormat
Set tb = olef.Object
tb = lbl_test
lbl_test.Caption = ""
lbl_test.BackColor = RGB(255, 255, 255)

如果InlineShapes(1(引用的InlineShape是而不是MSForms.Label,则在"Set ils"行中会出现类型不匹配错误。因此,您的代码可能使用了错误的形状。如果InlineShapes(1(MSForms.Label,这应该有效:

Dim ils As Word.InlineShape
Dim olef As Word.OLEFormat
Dim tb As MSForms.Label
Set ils = ActiveDocument.InlineShapes(1)
Set olef = ils.OLEFormat
Set tb = olef.Object
tb.Caption = ""
tb.BackColor = RGB(255, 255, 255)

这条线路

tb = lbl_test

可能在做一些与你想象的截然不同的事情。放置

lbl_test.Caption=">

之后,它表明您正在尝试创建对MSForms.Label对象的引用,但我猜lbl_test是一个字符串变量(我们从您提供的代码片段中看不到(。如果是,尝试设置

lbl_test.Caption

也会导致错误。

什么

tb = lbl_test

实际操作是将lbltest的值分配给tb对象的"默认成员"。对于MSForms.Label对象,默认成员是.Caption(您可以在VB编辑器的对象查看器中查找它(,因此实际上该行应该设置标签的标题。但最好的做法是不依赖默认成员,并将其拼写为

tb.Caption = lbl_test

有几种方法可以访问文档表面中的ActiveX控件。有一个,但如果你碰巧知道它的名称,你也可以直接从ThisDocument访问控件,例如通过ThisDocument.Label1。那么你就不需要知道控件的类型了。

但除此之外,您必须在执行操作时访问InlineShape或Shape。因为不同的ActiveX控件确实有不同的成员(例如,标签有标题,但TextBox没有(,所以在使用对象类型之前,您最好先检查对象类型,也许是这样。。。

Dim ils As Word.InlineShape
Set ils = ActiveDocument.InlineShapes(1)
If Not (ils.OLEFormat Is Nothing) Then
With ils.OLEFormat
Select Case .ClassType
Case "Forms.Label.1"
' You don't even need to assign to an object variable
With .Object
.Caption = ""
.BackColor = RGB(255, 255, 255)
End With
Case "Forms.TextBox.1"
' do the appropriate thing
Case "Forms.CheckBox.1"
' do the appropriate thing
Case Else
' e.g. it might not be an ActiveX control
' perhaps do nothing
End Select
End With
End If