我如何以ActiveX Label inlineshape为目标以更改背景色并隐藏标签中的文本

问题描述 投票:0回答:1

我刚刚在可用于ActiveX文本框控件的网站上找到了此代码。我希望它在标签ActiveX控件上工作,当我提交命令按钮控件时,标签中会发生两件事:标签从红色变为白色And .caption =“”。 (我的目标是隐藏标签)。

我正在尝试的代码可用于文本框控件,但不能用于标签,因为我认为它们是不同的数据类型? (出现不匹配错误)。您能提供帮助-谢谢您吗?

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

    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)
ms-word word-vba
1个回答
0
投票

如果InlineShapes(1)引用的InlineShape为MSForms.Label not,则在“ Set ils”行中将出现Type Mismatch错误。因此,也许您的代码使用了错误的形状。如果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

也会导致错误。

What

tb = lbl_test

实际上是将lbl_test的值分配给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
© www.soinside.com 2019 - 2024. All rights reserved.