Call add-in method from macro?

D

Duncan McNiven

I am using Delphi (2009) to write my first add-in for Outlook (2007). I want to add a custom method to my add-in's class and call it from an Outlook macro, but I can't get it working.
My method in my Delphi class is public. It looks like this:

procedure MyMethod(const Index : integer); safecall;

I tried to call it from Outlook like this:

Sub Test_02()
Application.COMAddIns.Item("<My Add In>").Object.MyMethod (3)
End Sub

I get:
Run-time error '91':
Object variable or With block variable not set


I then tried:

Sub Test_01()
Dim MyAddIn As Office.COMAddIn

Set MyAddIn = Application.COMAddIns.Item("<My Add In>")
If Not (MyAddIn Is Nothing) Then
If MyAddIn.Connect = True Then
Dim MyObject As Object
Set MyObject = MyAddIn.Object
If Not (MyObject Is Nothing) Then
MyObject.MyMethod (3)
End If
End If
End If
End Sub

This fails because MyObject is Nothing, so the call to MyObject.MyMethod doesn't happen.

My next attempt was:

Sub Test_03()
Dim MyObject As Object
Set MyObject = CreateObject("<My Add In>")
If Not (MyObject Is Nothing) Then
MyObject.DoNewMail (Null)
End If
End Sub

The DoNewMail method is the handler for Outlooks OnNewMail event, and is called correctly when new mail is received. I guess CreateObject would create a new object rather than use the one already instantiated in my Add-In, which is not what I want, but I was trying everything at this stage. Anyway, this attempt failed with:

Run-time error '438':
Object doesn't support this property or method

So where am I going wrong?
 
D

Duncan McNiven

After being stuck on this for days I hit on a solution.
In my OnConnection event in the Delphi code I put something like:

var
SelfDisp : OleVariant;
begin
SelfDisp := Self as IMyClassDisp;
OleVariant(AddInInst).Object := SelfDisp;

(IMyClassDisp is a descendant of IDispatch).
Then the IMyClassDisp methods included in the type library can be called from a macro using the same syntax I had tried earlier.

Just thought I would post this note in case anyone else finds this thread whilst stuck on the same problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top