Outlook automation using managed code

M

Max

Hi,

I would like to have a button and a combo box with options to select various versions of Microsoft Outlook: 2002, 2003.
The user selects the email client and clicks the button. The only things happening will be: a new email is created and some text is
placed in the body, then the email window is displayed. The user can close the window or select some recipients and send the
message.
I would like some info on how to do this correctly. Is the best way to do this using early binding or late binding?

If I work with early binding, then what is the correct way of doing this? Right now I installed the Office 2003 PIAs from the link
below:

http://www.microsoft.com/downloads/...3A-AC14-4125-8BA0-D36D67E0F4AD&displaylang=en

and I added a reference to Microsoft Outlook 11.0 Object Library to my project. Was this the right way to do it? Is installing the
above the same as getting the Office 2003 PIAs through the Office 2003 setup feature? I am beginning to think that it is not and I
have to manually remove these from the GAC and do a repair and get them through Office 2003.

For what I need to do which is just open an email and put some text in it then let the user continue, will the 2003 version of
Outlook PIA be enough to support both MS Outlook 2002 and 2003 or do I have to rely on late binding? The setup project is adding
dependencies to the dlls used and they go with the application. Is this the right way to do it or do I need to ask the user to
download the PIA's from the link above as well?

Thanks,
Max
 
P

Pete Davis

You'll need to use late binding. You can use early binding, but then you'll
have to bind to all the different versions.

Personally, I've found binding to Outlook (and other Office app) stuff to be
a pain in the ass because of the different versions and the massive changes
that happen from version to version. It tends to require very different code
for each version which can be a big pain.

Pete
 
M

Max

Thanks for your reply Pete.

I have successfully used late binding to automate Outlook 2000,2002,2003. However I did not have any event handlers for catching
when the user closed the email window or sent the email and hence Outlook remained in memory since I did not clean up :)
Now that I'm trying to work my way through Inspectors and Creating Delegates I'm getting run time errors....
 
M

Max

The error I'm getting at runtime says:

System.ArgumentException: Error binding to target method.
at System.Delegate.InternalCreate(Object target, String method, Boolean ignoreCase)
at System.Delegate.CreateDelegate(Type type, Object target, String method)

and it happens on the last line of this code:

Code:
Type oType = Type.GetTypeFromProgID("Outlook.Application");
oApp = Activator.CreateInstance(oType);

oInspectors = oApp.GetType().GetProperty("Inspectors").GetValue(oApp, null);

e_NewInspector = oInspectors.GetType().GetEvent("NewInspector");
d_NewEventDelegate = Delegate.CreateDelegate(e_NewInspector.EventHandlerType,
this, "Inspectors_NewInspector");

The debugger loads the assemblies from the GAC. Is there anything wrong with that?

Loaded 'c:\windows\assembly\gac\microsoft.office.interop.outlook\11.0.0.0__71e9bce111e9429c\microsoft.office.interop.outlook.dll',
No symbols loaded.
Loaded 'c:\windows\assembly\gac\office\11.0.0.0__71e9bce111e9429c\office.dll', No symbols loaded.
 
P

Pete Davis

Honestly, it would be hard to say. The last time I used outlook was a couple
versions back (wrote an app that notified me of new mail with a voice
synthesizer that spoke the name and subject of the e-mail).

The assemblies you're using are definitely different than the ones I used. I
don't recall creating any delegates, per se. The only thing I did was
subscribe to the NewMailEventHandler which was as simple as something like
this:

applicationObject.Application.NewMail +=
new
Outlook.ApplicationEvents_NewMailEventHandler(Application_NewMail);

From my version of the API, I would think that all you'd have to do is:

oApp.Application.Inspectors.NewInspector +=
Outlook.InspectorEvents_NewInspectorEventHandler(My_NewInspector)

and then add an event handler called My_NewInspector.

Now, maybe I did mine differently than you're doing yours.

I had a class that implemented Extensibility.IDTExtensibility2. It also had
a ProgID and Guid and was registered for COM interop.

Honestly I don't remember the specifics. I wrote this thing back in 2003.

If you'd like to look at the code, though, you can download it here:
http://www.petedavis.net/downloads/SpokenMail.zip

Pete


Max said:
The error I'm getting at runtime says:

System.ArgumentException: Error binding to target method.
at System.Delegate.InternalCreate(Object target, String method, Boolean
ignoreCase)
at System.Delegate.CreateDelegate(Type type, Object target, String
method)

and it happens on the last line of this code:

Code:
Type oType = Type.GetTypeFromProgID("Outlook.Application");
oApp = Activator.CreateInstance(oType);

oInspectors = oApp.GetType().GetProperty("Inspectors").GetValue(oApp,
null);

e_NewInspector = oInspectors.GetType().GetEvent("NewInspector");
d_NewEventDelegate =
Delegate.CreateDelegate(e_NewInspector.EventHandlerType,
this, "Inspectors_NewInspector");

The debugger loads the assemblies from the GAC. Is there anything wrong
with that?

Loaded
'c:\windows\assembly\gac\microsoft.office.interop.outlook\11.0.0.0__71e9bce111e9429c\microsoft.office.interop.outlook.dll',
No symbols loaded.
Loaded
'c:\windows\assembly\gac\office\11.0.0.0__71e9bce111e9429c\office.dll', No
symbols loaded.




Max said:
Thanks for your reply Pete.

I have successfully used late binding to automate Outlook 2000,2002,2003.
However I did not have any event handlers for catching when the user
closed the email window or sent the email and hence Outlook remained in
memory since I did not clean up :)
Now that I'm trying to work my way through Inspectors and Creating
Delegates I'm getting run time errors....
 

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