Microsoft
Software
Hardware
Network
Question : VBA ole object wrapping
Hi,
I'm trying to wrap a OLE object into a class (such as I can add my own events etc.)
Unfortunately, it seems to only work if the OLE Object is manually created.
If it is created in my class or even in the macro that use the object, it goes out of scope at the end of the macro and induces the termination of my wrapping object, even if my object is a global variable.
The following shows this point:
class module called Obj:
Private p_oleCtl As OLEObject
Property Set ActiveXControl(ByVal oleCtl As OLEObject)
Set p_oleCtl = oleCtl
End Property
Property Get ActiveXControl() As OLEObject
Set ActiveXControl = p_oleCtl
End Property
Private Sub Class_Terminate()
MsgBox "end"
End Sub
Regular module:
Dim GlobalObj As Obj
Sub CreateButton()
Call ActiveSheet.OLEObjects.Add
(ClassType
:="Forms.C
ommandButt
on.1")
End Sub
Sub Test()
Call CreateButton
Set GlobalObj = New Obj
Set GlobalObj.ActiveXControl = ActiveSheet.OLEObjects(1)
End Sub
when Test is run, the msgbox "end" is displaid.
It is not displaid if I run CreateButton first and then Test (without the call to CreateButton).
Strangely enough, The same issue occurs even if the wrapped OLE object is a global variable.
Any clue as to why this is like that and how to work around it?
Many Thanks
Answer : VBA ole object wrapping
By adding an activex object you effectively trigger a code reset (because they become part of the vbproject). If you really need to do this, I would suggest using an OnTime macro to assign the variable after you have created it.
Random Solutions
XP Pro SP3 on Dell Inspiron 9400 notebook with OCZ SSD: BSOD 0xF4 after resume from standby
show crystal report on vb.net (database : Oracle)
simple math programming question
How to alter tablespace paramter SEGMENT SPACE MANAGEMENT manual to auto in oracle
Array Controller got crashed after replacement of battery..
Outlook not attack shortcut (lnk) attach directly the file
SAP books for implementing ERP package
Check to see if a table has a certain value, MS Access 2003
How to get rid of dirty shut down state when recovering edb file
Diffrent vb.net and Sql Server MD5 hashing