【发布时间】:2016-08-15 02:47:25
【问题描述】:
我有一个充满 ActiveX 控件的工作表。我知道它们在 Worksheets 上存在问题,所以这是我实现的功能,用于重置控件并检查它们的行为。
问题是错误处理程序没有捕获由于尝试访问列表框的自动大小属性而发生的运行时错误 438(列表框没有自动大小属性,因此运行时 438 错误)。
Public Sub RefreshCtrlSheet(Optional ByRef HiddenElements As Variant)
'This sub refreshes ActiveX Objects on the Ctrl Sheet
Dim objX As Object 'Holds OLEObjects from Control Sheet
Dim tempWidth As Double 'Store size of object to restore
Dim tempHeight As Double 'after resetting
Dim tempLeft As Double
Dim tempTop As Double
Dim i As Integer
With Sheet1
'Error Handler
On Error GoTo NotTheObjsUrLooking4
For Each objX In .OLEObjects
'If Name has Button in it
If InStr(objX.Name, "Button") > 0 Then
'Implement different button sizes
If objX.Name = "LoadDataButton" Then
tempHeight = 24.75
tempWidth = 24.75
Else
tempHeight = 30
tempWidth = 80
End If
Else
tempHeight = objX.Height
tempWidth = objX.Width
End If
'Check to see if elements should be hidden
If Not IsMissing(HiddenElements) Then
For i = 1 To UBound(HiddenElements)
If InStr(objX.Name, HiddenElements(i)) Then
objX.Visible = False
End If
Next i
End If
tempLeft = objX.Left
tempTop = objX.Top
'Autosize to refresh shingking controls (ActiveX Bug)
objX.object.AutoSize = True '----------------------RUNTIME ERROR HERE!
objX.object.AutoSize = False
objX.Width = tempWidth
objX.Height = tempHeight
objX.Left = tempLeft
objX.Top = tempTop
NotTheObjsUrLooking4: '----------------------------THIS WORKS EVERY OTHER ERROR
Next objX
On Error GoTo 0
End With
End Sub
当我添加一个新的 ActiveX 列表框时,它开始出现问题,这很奇怪,因为我已经有了另一个,但从未出现过问题。
感谢任何人提供的任何帮助!
编辑:
谢谢Mat,就是这样。
新代码适用于:
...
'Error Handler
On Error GoTo CleanFail
...
'Other code
...
NotTheObjsUrLooking4:
Next objX
End With
On Error GoTo 0
Exit Sub
CleanFail:
Err.Clear
Resume NotTheObjsUrLooking4
End Sub
【问题讨论】:
标签: vba foreach error-handling runtime-error activex