【问题标题】:Checking if an OLE Object exists in a worksheet检查工作表中是否存在 OLE 对象
【发布时间】:2019-01-22 13:44:35
【问题描述】:

我正在处理需要用户在 Excel 中附加文件的用户表单。

我正在尝试在用户表单中构建一个控件。我在表单中放置了一个“保存并关闭”命令按钮,用于检查用户是否填写了所有字段并附加了所有附件。

我在检查工作表中的附件时遇到问题。如何让 VBA 检查用户附加的工作表中是否存在任何 OLE 对象?

'下面是我检查和输入文件的代码

Private Sub SAC_Click()
    If CLN.Text = "" Then
        MsgBox ("Company Legal Name is a mandatory field!")
    Else
        Range("A2").Value = CLN.Text
    End If

    If BRL.Text = "" Then
        MsgBox ("Business Registration/ License is a mandatory field!")
    Else
        Range("B2").Value = BRL.Text
    End If

    If COA.Text = "" Then
        MsgBox ("Company Address is a mandatory field!")
    Else
        Range("C2").Value = COA.Text
    End If

    Range("D2").Value = PON.Text
    Range("E2").Value = TNR.Text

    If BLA.Text = "" Then
        MsgBox ("Billing Address is a mandatory field!")
    Else
        Range("F2").Value = BLA.Text
    End If

'VAT Registration data

    If VRN.Text = "" Then
        MsgBox ("VAT Registration Number is a mandatory field!")
    Else
        Range("G2").Value = VRN.Text
    End If

If VRD.Text = "" Then
        MsgBox ("VAT Registration Date is a mandatory field!")
    Else
        Range("H2").Value = VRD.Text
    End If

If COR.Text = "" Then
        MsgBox ("Country of Registration is a mandatory field!")
    Else
        Range("I2").Value = COR.Text
    End If

'Finance information data
'Payment Terms
If PmtTerms.Text = "" Then
        MsgBox ("Payment Terms is a mandatory field!")
    Else
        Range("J2").Value = PmtTerms.Text
    End If

If PmtMtd.Text = "" Then
        MsgBox ("Method of Payment is a mandatory field!")
    Else
        Range("K2").Value = PmtMtd.Text
    End If

'Finance other contacts

If NAS.Text = "" Then
        MsgBox ("Name of Authorized Signatory is a mandatory field!")
    Else
        Range("L2").Value = NAS.Text
    End If

If EMA.Text = "" Then
        MsgBox ("E-mail address is a mandatory field!")
    Else
        Range("M2").Value = EMA.Text
    End If

If MNR.Text = "" Then
        MsgBox ("Mobile number is a mandatory field!")
    Else
        Range("N2").Value = MNR.Text
    End If

If FCF.Text = "" Then
        MsgBox ("Finace contact is a mandatory field!")
    Else
        Range("O2").Value = FCF.Text
    End If

If EMA2.Text = "" Then
        MsgBox ("Finace E-mail address is a mandatory field!")
    Else
        Range("P2").Value = EMA2.Text
    End If

If MNR2.Text = "" Then
        MsgBox ("Finace mobile number is a mandatory field!")
    Else
        Range("Q2").Value = MNR2.Text
    End If

'Business contact information

    Range("R2").Value = CFN.Text
    Range("S2").Value = EMA3.Text
    Range("T2").Value = MNR3.Text

'Testing saving control


End Sub

【问题讨论】:

  • 您是否正在检查是否存在任何 OLE 对象?
  • 另外,您不需要为表单中的每个TextBox 设置If 条件。您可以只使用一个循环来遍历表单中的所有字段,并且只检查 TextBox 中的空值。将您的代码减少到大约 10 行。看看this
  • @SJR 是的,在用户单击“保存并关闭”按钮之前,我正在检查工作表中是否存在 OLE 对象。

标签: excel vba object ole


【解决方案1】:

如果工作表中没有 OLEObject,ThisWorkbook.Sheets(1).oleobjects.Count 将为零。如果你有一些现有的对象,那么只要你知道你开始有多少个对象,那么你可以检查它是否增加了一个以确定是否添加了一个

【讨论】:

  • 嗨,泰勒,感谢您的快速回复。如果我假设我必须对象怎么办?
  • Harassed 已经回答了以下问题:如果我认为必须(我怀疑您的意思是 two)对象怎么办​​。检查启动工作簿时的计数。然后您可以检查是否添加了一个。如果您需要其他检查,您可以遍历 OLE 对象并获取它们的详细信息
猜你喜欢
  • 1970-01-01
  • 2018-09-08
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
  • 2022-11-15
  • 2012-07-27
  • 2013-09-26
相关资源
最近更新 更多