Search code examples
vbscriptmfccomole

MFC Automation events are working in Excel VBA but not in VBScript


I have created an MFC automation application and also added a source interface (outgoing interface) for events. I can create the COM object and get the event notifications in Excel VBA, but not in VBScript. I checked the MFC application multiple times if something is wrong, but everything seems correct. I tried Dialog based and SDI both, but both have the same problem. I tried with an ATL project which works absolutely fine in both (VBA and VBScript).

Here is Excel VBA - this works:

Private WithEvents foo As Automate

Private Sub CommandButton1_Click()
    Set foo = New Automate
    foo.Show
End Sub

Private Sub foo_OnShow()
    MsgBox "VBA Event called Succeeded"
End Sub

And here is VBScript - this does not work:

Set obj = WScript.CreateObject("Automate.Application", "ob_")
obj.Show()

Sub ob_OnShow()
    MsgBox "Automate Client: OnShow"
End Sub

The VBScript creates the object and call the method Show() when the second parameter ob_ is not passed. But when ob_ is passed, the script creates the object, queries for IDispatch successfully, and then releases the object for some reason. I tried changing the Sub to Function too. I get the following error:

enter image description here

Any ideas? Thanks all!

Environment: Win11, Visual Studio 2022


Solution

  • Found the problem.

    The following method is overridden:

    virtual BOOL GetDispatchIID(IID* pIID);
    

    which is returning IID_IDispatch rather then IID_IAutomate.