Intercepting new outgoing e-mails

G

Guest

Hello -

I want to dynamically determine the contents of my e-mail signature at the
time a new e-mail is composed. I can't seem to find any objects in the PIA
that deal with signatures, and while it seems that I could create a new
e-mail item in a rather straightforward way and then display its inspector,
the only way I could think to do this is by replacing the "New" button in the
command bar and the "New" menu item in the main menu.

The other difficulty with this solution (the replacement of the button and
menu items) is that the command bar button is dynamic; sometimes it's for
e-mail, and other times it's for tasks, appointments, etc. And, this would
only intercept New e-mails; I would have to do something similar for Reply,
Reply to All, and Forward, not only on the command bar, but also on the
context menu.

Is there any kind of event that happens when a new Inspector appears?

I'm currently using Outlook 2007 and Visual C#; however, if this
functionality could only be achieved with ATL and C++, I could move to that.
 
K

Ken Slovak - [MVP - Outlook]

NewInspector of the Inspectors collection fires when a new Inspector is
created.

Inspector.CurrentItem.Body is the text of the email.

You can create a new email item using Folder.Items.Add() or
Application.CreateItem().

Take a look at one of the Outlook 2007 sample addins at
http://www.slovaktech.com/outlook_2007_templates.htm or various samples on
www.outlookcode.com for various samples for shared and VSTO addins using C#.
Outlookcode also has quite a number of other C# samples.
 
G

Guest

Shortly after posting this I found the NewInspector event. However, now that
I've wired into it, it seems like it only fires the first time an Inspector
is created, because this Inspector seems to be reused. (I'm storing my
plugin's instance in a static object, then accessing the Application object
through my plugin's instance. Application.Inspectors.Count is always 1).

I can click "New Email" all day long and it's only firing the first time I
start Outlook.

In addition, I did some work to see how well I could exploit this event for
my purposes. Let's say I wanted to append dynamic text to my signature
(which was my stated goal).

Take a look at: http://robpaveza.net/pub/emailModified.png

What that's showing is a reply e-mail. I hit reply and intercepted the new
Inspector. I then directly modified the HTMLBody property of the MailItem
object, prepending an HTML paragraph with "This is some extra text" as its
content, immediately before the div element containing the very first text in
the e-mail (immediately following the CSS and body tag).

Do you happen to have a particular code sample in mind that I could take a
look at?

Thanks for the prompt reply.

--Rob
 
G

Guest

OK, I found the solution to the problem about the Inspector not creating new
instances. Apparently, registering for event notification prevents the
inspector from ever being released by the marshaler, which therefore
prevented new ones from being made (I guess).

Inspector.PageChange += new
InspectorEvents_10_PageChangeEventHandler(Inspector_PageChange);

Commenting out that line allowed NewInspector to fire a bunch of times.

The problem still exists - how do I get to my signature?
 
K

Ken Slovak - [MVP - Outlook]

NewInspector will fire for every new added Inspector whether or not you
release your event handler. I have no idea how you did your code but as I
suggested earlier take a look at the samples I provided links for to see how
to handle things correctly. Normally you would use an Inspector wrapper
class and a collection to hold open Inspectors because you can have more
than one open at a time. If you don't do that you can't handle that
situation correctly. You release your Inspector objects and the event
handlers in Inspector.Close(), but you certainly can handle events for more
than 1 Inspector at a time, I do it all the time.

Why you're handling that event (PageChange) I have no idea. I don't think it
will do anything for you.

There is no signature property, you must scan the entire body of the message
or parse the existing HTML from HTMLBody to find the signature. If the
message is HTML you can load HTMLBody into an HTML document object and parse
that, but no matter what you do or how you handle it you must figure out for
yourself where the signature is and what it is. There aren't any helpers for
that.
 

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