Calling a CommandButton's default action after an addin overrides it

S

Stuart Langridge

I'm writing an Outlook addin that pops up a form when the user hits "Send"
while composing an email. Once the form is completed, I would like to have
the form close and then the default action for the button (sending the mail)
take place. Can I do this? I can, for example, directly call the MailItem's
Send method, but that doesn't do the same thing as clicking the Send button
in the Inspector -- for example, an empty "To" field is not trapped. I don't
want to have to replicate everything that Outlook does when clicking Send in
my addin if I can avoid it -- how do I override a CommandButton to do my
custom action and then do its default action?

Stuart Langridge
Information Architect
Mills & Reeve
 
K

Ken Slovak - [MVP - Outlook]

Once you use the ID of a built-in button it would act just like the standard
button acts.

When I intercept either MailItem.Send or Application_Send I just do whatever
I want and then let the event proceed. Cancel does work, you just set it
equal to True, but in this case you wouldn't want to do that.

If you are adding recipients to a message in Send you just have to resolve
all the recipients and test for any that are unresolved. Not very hard.

If you are calling the Send method of an item, or using automation to click
a Send button (by calling its Execute method) you will find that the action
will trigger the security prompts for secure versions of Outlook unless the
addin is a trusted COM addin. You don't mention which version or versions of
Outlook you intend to support, so I can't really provide more information on
that, but there are workarounds such as using Redemption code (3rd party
library located at www.dimastr.com/redemption) or writing the code as a
trusted addin for Outlook 2003 for example.
 
S

Stuart Langridge

Ah, here's the difference. I'm actually changing the action on the Send
button in my addin. I hook the NewInspector event and then find the Send
button (with oCurrentInspector.CommandBars("Standard").FindControl(, 2617))
and override its Click event handler to show my form:

Private Sub btnSend_Click(ByVal Button As office.CommandBarButton, ByRef
cancelDefault As Boolean)
Set frmGems.inspector = Button.Application.ActiveInspector
frmGems.Show
cancelDefault = False
End Sub

However, the built-in action of that button (i.e., sending the mail) doesn't
happen, even if the form is unloaded. Imagine that the form had one big
button on it, with "Send" written on the button. So I want to click the
Outlook Send button, have it display my form, and then when the user clicks
"Send" on my form, it should send the mail. How can I do that? Do I have to
invoke the MailItem's .Send method? Or can I just "fall through" to the
default action? I thought that that's what setting cancelDefault to False in
the Click event handler would do, but it doesn't seem to.

sil
 
K

Ken Slovak - [MVP - Outlook]

If you set the cancel argument to False you are saying not to cancel the
action. Showing a form the way you are showing it allows the code to
continue right away. If you want the form code to run and do whatever then
you would show the form modally. If you just want your form to be displayed
and then have the original Send button action canceled you would set cancel
to True. Then in your form the Send button would call Send on the item but
you can't call that from within the same event handler, it's not re-entrant.
 
S

Stuart Langridge

I am clearly not understanding what you're saying here. My understanding of
what you've written below is that, as soon as I click Send, *two* things
should happen: my form should appear, *and* the email should be sent
(because I have not cancelled the built-in action). This isn't the case, so
clearly I didn't correctly understand your explanation :) I would like my
form to be displayed, and then when my form closes, I want the default Send
action (i.e., sending the mail) to happen. I thought that this *would*
happen because I've set cancelDefault to false in btnSend_Click, meaning
that I want the default action to go ahead. Do you think you could explain
again why it doesn't? Sorry :)

sil
 
K

Ken Slovak - [MVP - Outlook]

Cancel = False means don't cancel the action, in this case a send. Cancel =
True means to cancel the action (send). However if you are doing some
processing and just want the send to continue after your own code then I
never even bother setting Cancel.

Opening a form non-modally opens the form and then continues on with the
next code line after that. Opening it modally stops execution of the code in
the procedure at the point where the form is opened and doesn't execute the
code in the next line in the procedure until after the modal form is closed.

I'm not sure why your sending isn't happening but see if it does if you
don't bother to set Cancel at all. FWIW, I wouldn't do things as you're
doing them. I wouldn't care how the user ending up sending the mail item.
I'd just handle the MailItem.Send or even Application.Send instead, do my
processing and then let the send continue.
 

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