PC Review


Reply
Thread Tools Rate Thread

Call add-in method from macro?

 
 
Duncan McNiven
Guest
Posts: n/a
 
      17th Nov 2009
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?
 
Reply With Quote
 
 
 
 
Duncan McNiven
Guest
Posts: n/a
 
      17th Nov 2009
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.
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call method from FormView_Inserting method sck10 Microsoft ASP .NET 0 14th Mar 2007 04:29 PM
newbie: How to call a method from a static method? deko Microsoft C# .NET 7 26th Feb 2006 08:41 AM
GetObject method not work after Call Shell Method =?Utf-8?B?YmVu?= Microsoft Excel Programming 8 21st Feb 2006 03:45 PM
Warning 1684 CA2214 : Microsoft.Usage : 'RandomShade..ctor(Int32, Int32, Int32, Int32, Int32)' contains a call chain that results in a call to a virtual method defined by the class. Review the following call stack for unintended consequences: steve bull Microsoft C# .NET 4 7th Jul 2005 05:54 PM
static method call vs. Singleton method call - performance Victor Jones Microsoft C# .NET 1 22nd Aug 2003 03:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:56 PM.