Link to home
Start Free TrialLog in
Avatar of boncqdcaveut
boncqdcaveutFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.CommandButton.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
ASKER CERTIFIED SOLUTION
Avatar of Rory Archibald
Rory Archibald
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of boncqdcaveut

ASKER

My ultimate goal is to use the MVC pattern and be able to replace Excel by HTML should I need to.
As a result my controller(s) should not be system dependent (they could be implemented with OLE buttons, but also some other types of controls).

Thus I want to avoid using VBE extensibility (kind of meta programming) as that would create new worksheet modules, be dependent of the names of the controls, be less portable etc.

Thanks to your solution, I can create controls at run-time but still use code written statically  to handle the control events.

Below a simple example using your work-around:


1) Class module called "Obj"
Private WithEvents p_oleCtl As MSForms.CommandButton
Property Set ActiveXControl(ByVal oleCtl As MSForms.CommandButton)
    Set p_oleCtl = oleCtl
End Property
Property Get ActiveXControl() As MSForms.CommandButton
   Set ActiveXControl = p_oleCtl
End Property
Private Sub Class_Terminate()
MsgBox "end"
End Sub
Private Sub p_oleCtl_Click()
MsgBox "It works"
End Sub

2) Regular module:
Dim GlobalObj As New Obj
Sub CreateButton()
Call ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1")
End Sub
Sub AssignLater()
Set GlobalObj.ActiveXControl = ActiveSheet.OLEObjects(1).Object
End Sub
Sub Test()
Call CreateButton
Application.OnTime Now + TimeValue("00:00:01"), "AssignLater"
End Sub

Now running Test allow me to see "It works" be displaid whenever I click the new button.

So, thank you very much rorya.
I'm just waiting to see if somebody comes up with a maybe "cleaner" solution before granting you the points.

Best,
phil

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All right, too bad.

Thanks again.
On that basis you'll be waiting a very long time for another answer from me.
rorya,

There has been a misanderstanding here (I haven't used this site before).

I tryed to give you the points but I got this strange "Close Request Pending" stuff.
Mybe because I gave "only" a grade B.

This grade is for the "Overall experience" and has nothing to do with you (and it isn't aim at complaining about this great website neither! it just that I didn't enjoy the experience 100% as I had to wait - probably as I didn't choose the best zones -)

I read your profile and really admire your skills and the fact you're helping other people for free.

Sincerely sorry if I offended you in any way.

Best regards,
Phil

Not to worry - I was having a bad day, and hadn't noticed you weren't necessarily too familiar with the site.
For the record, the grade you give directly affects the number of points awarded to the answerer; it has no other effect at all that I am aware of. :)
Hi,

I'd like to accept a solution and obviously "closing a question" does not mean that.

Please allow me to cancel my request to close this question.

Thanking you in advance,
Phil