首页 > 解决方案 > 当工作表处于非活动状态或窗口最小化时,无法设置 OLEObjects 属性

问题描述

我正在尝试通过在工作表中设置一些 OLE 对象的属性来初始化 Excel 工作表,并将下面的代码放在标准模块中。

With ThisWorkbook.Worksheets("UserView")
    .btnShowAllItems.Enabled = False
    .btnShowAllItems.Visible = False
    .btnAvailableOnly.Enabled = True
    .btnAvailableOnly.Visible = True
    .AutoFilter.ShowAllData
End With

上面的代码按预期工作,除非在 Excel 窗口最小化或用户在不同的工作表中调用 sub 时,导致以下错误。

Run-time error '1004':
Unable to set the Enabled property of the OLEObject class

我究竟做错了什么?当 Excel 窗口最小化或用户在不同的工作表中时,如何设置属性?

标签: excelvba

解决方案


.Object打电话时尝试添加Enabled- 为我解决了这个问题

With ThisWorkbook.Worksheets("UserView")
    .btnShowAllItems.Object.Enabled = False
    .btnShowAllItems.Visible = False
    .btnAvailableOnly.Object.Enabled = True
    .btnAvailableOnly.Visible = True
    .AutoFilter.ShowAllData
End With

推荐阅读