MailItem won't close within ItemSend

G

__geof__

Hi.

I have written a little AddIn for Outlook 2007 and I try to adapt it to
Outlook 2003. It is looking good but I have a problem when closing the
MailItem. Here is a sample of the code:
Case Windows.Forms.DialogResult.Yes
Cancel = True
currentMailItem.Close(1)
Exit Select
Case Windows.Forms.DialogResult.No
Cancel = False
Exit Select

It says that I cannot close within ItemSend. I looked around and I also
tried with currentMailItem.GetInspector.Close(1) without any luck (just
another error).

Anyone has an idea?

Tx


Geoffrey
 
K

Ken Slovak - [MVP - Outlook]

I'd be amazed if closing the item within Item.Send() actually works without
exceptions in Outlook 2007 either. The best way to close the item from
Send() is to set a timer to fire in the Send() event and when it fires call
to close the item. I usually close the Inspector rather than the item itself
though, it closes the item also and doesn't leave a ghost Inspector artifact
window, which can happen sometimes when the item is closed and not the
Inspector.
 
G

__geof__

Right you are. In 2007 it is the Inspector that is closed and it works fine
but it does work in 2003 (for me). I get an error saying: "Operation aborted.
(Exception from HRESULT: 0x800004004 (E_ABORT))."

About setting a timer in the Send() Event... How do I do that? Could you
give me a direction? I am (obviously) quite new to .NET and Office
Development.
 
K

Ken Slovak - [MVP - Outlook]

In the class that handles that Inspector you can add this sort of
declaration at the class level:

Private WithEvents _shutdownTimer As System.Timers.Timer = Nothing

In the Send() code you'd have something like this at the point where you
decided you want to close down the Inspector using the timer:

If (_shutdownTimer Is Nothing) Then
_shutdownTimer = New System.Timers.Timer()

With _shutdownTimer
.Enabled = False
.AutoRefresh = False
.Interval = 1000 ' 1 second
.Start()
End With

That Start() call should be made just before the Send() code terminates. In
the timer event handler you then close the Inspecgtor. In the
Inspector.Close() event you check to see if the timer is Nothing and if not
call Dispose() on it and set it to Nothing as part of your Inspector cleanup
code.

The timer elapsed event handler signature will be something like this:

Private Sub _shutdownTimer_Elapsed(ByVal sender As Object, _
ByVal e As System.Timers.ElapsedEventArgs)
 
G

__geof__

Hi Ken.

Thanks for that. It works just fine. It took me some times to implement it
since I'm quite new to .NET but it is doing what it is supposed to.

Thanks again
 

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