Private WithEvents oCmb As CommandButton
Private Sub oCmb_Click2()
MsgBox "Hello from " & "'" & oCmb.Caption & "'"
End Sub
Public Function MakeTheButton()
Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102)
Set oCmb = Cmb
End Function
Private oCmbInstance As InventoryUpdater_Class
Sub AddButton2()
Set oCmbInstance = New InventoryUpdater_Class
oCmbInstance.MakeTheButton
End Sub
Sektor: I'm going to stick with what I know for now, but thanks for the input.
Jaafar: There's four or five things going on in your code that are turning me for a loop. I'm working through most of it, but I'm left wondering about a couple of things.
1) I get an error when I step through the "AddButton" procedure. This makes it really hard to debug. Any idea why this is happening?
2) I've never worked with a shape range obejct before and I would prefer to work with the buttons' names if at all possible. But I can't seem to get the code to recognize the name of the button alone. Is it necessary to use a named range to refer to the button in your code?
Runs successfully, but fails to hook button:
Code:Option Explicit Private WithEvents oCmb As CommandButton Private Sub oCmb_Click() MsgBox "Hello from " & "'" & oCmb.Caption & "'" End Sub Public Property Set GetCommandButton(ByVal Cmb As CommandButton) Set oCmb = Cmb End Property
Code:Private oCmbInstance As CmbClass Sub AddButton() Dim Cmb As OLEObject Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _ (ClassType:="Forms.CommandButton.1", _ Height:=32, _ Width:=200, _ Left:=292, _ Top:=34, _ Width:=102) Cmb.Name = "MyCommandButton" Call HookButton End Sub Private Sub HookButton() Set oCmbInstance = New CmbClass Set oCmbInstance.GetCommandButton = ActiveSheet.OLEObjects("MyCommandButton").Object End Sub
Call HookButton
Application.OnTime Now + TimeSerial(0, 0, 1), "HookButton"
Wouldn't any old buttons be unhooked when any new button was added then? Because the class module object variables would be reset.
Isn't a unique name enough of a unique identifier? Each time this code runs, the buttons appear on a new sheet that replaces the old one, so there should never be any conflict.
Private oCmbInstance As CmbClass
Sub AddButton()
Dim Cmb As OLEObject
Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102)
Cmb.Name = "MyCommandButton"
Application.OnTime Now + TimeSerial(0, 0, 1), "HookButton"
End Sub
Private Sub HookButton()
Set oCmbInstance = New CmbClass
Set oCmbInstance.GetCommandButton = ActiveSheet.OLEObjects("MyCommandButton").Object
End Sub
Private WithEvents oCmb As CommandButton
Private Sub oCmb_Click2()
MsgBox "Hello from " & "'" & oCmb.Caption & "'"
End Sub
Public Function MakeTheButton()
Set Cmb = ThisWorkbook.ActiveSheet.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102)
Set oCmb = ThisWorkbook.ActiveSheet.OLEObjects("CommandButton1").Object
End Function
Private oCmbInstance As InventoryUpdater_Class
Sub AddButton2()
Set oCmbInstance = New InventoryUpdater_Class
oCmbInstance.MakeTheButton
End Sub
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
pDest As Any, pSrc As Any, ByVal ByteLen As Long)
Private WithEvents oCmb As CommandButton
Private Sub oCmb_Click()
MsgBox "You clicked on : " & "'" & oCmb.Name & "'"
End Sub
Public Sub MakeTheButton()
Dim oTempSht As Worksheet
Dim lSheetPtr As Long
'get a memory pointer to the activesheet object.
lSheetPtr = ObjPtr(ThisWorkbook.ActiveSheet)
'copy the sheet memory location to our temp variable
CopyMemory oTempSht, lSheetPtr, 4
'add the runtime button.
Set oCmb = oTempSht.OLEObjects.Add _
(ClassType:="Forms.CommandButton.1", _
Height:=32, _
Width:=200, _
Left:=292, _
Top:=34, _
Width:=102).Object
'very important to clear the oTempSht object var.
'not doing it will crash the application !!!!!
CopyMemory oTempSht, 0&, 4
End Sub
Option Explicit
Private oCmbInstance As InventoryUpdater_Class
Sub MakeButton()
Set oCmbInstance = New InventoryUpdater_Class
oCmbInstance.MakeTheButton
End Sub
I had no idea VBA would recognize an object's address if you put it into an expression. What if you tried to use its window handle?
How exactly is this workaround getting around the problem? I'm not clear on why the failure is occurring in the first place during the code on post #17.
There's no runtime error, the button click event simply does not trigger.
Would you explain why that's the case? I really want to understand this better.