我有一个组合框,该组合框有多个与之相关的事件(例如,ComboBox1_Change、Click、GotFocus、LostFocus)。出于我的目的,我需要使用 ActiveX 控件。
对于一些背景:
当机器连接到投影仪时,我遇到了 ActiveX 控件出现故障的问题。其他人也有同样的问题,但没有解决方案。我发现解决此问题的最佳方法是创建一个用户可以单击的附加按钮,该按钮将删除 ActiveX 控件并重新创建它。
问题是(和我的问题)......当我这样做时,VBA 事件不再执行。该控件的名称完全相同。是否有一个简单的修复(例如我可以设置的 ActiveX 控件中的属性),这样我就不需要编写 VBA 代码来删除和重写现有的 VBA 代码(我什至不能 100% 肯定会解决这个问题) ?
最佳答案
这似乎对我有用,在工作表中:
从工作表上的 2 个命令按钮开始:CommandButton1
和 CommandButton2
.
您无法单步执行代码,但它会运行。
Option Explicit
Private Sub CommandButton1_Click()
Dim oldObj As OLEObject
Dim left As Double, top As Double, width As Double, height As Double
Set oldObj = Me.OLEObjects("CommandButton2")
With oldObj
'Rename the control to detach events
.Name = "CommandTEMP"
left = .left
top = .top
width = .width
height = .height
.Delete
End With
Dim newObj As Object
Set newObj = Me.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False _
, DisplayAsIcon:=False, left:=left, top:=top, width:=width, height:=height)
newObj.Name = "CommandButton2"
End Sub
Private Sub CommandButton2_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Me.CommandButton2.Object.Caption = Timer
End Sub
关于vba - 使用 VBA 重新创建带有 VBA 事件的 ActiveX 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249503/