ItemSend event not firing

  • Thread starter Thread starter PatrickS
  • Start date Start date
P

PatrickS

I have a support tool that I'm supposed to be maintaining. I'd like to be
able to have a button on the support call form that opens up an email in
Outlook (at this stage I can assume that it will only be Outlook
2000/2002/2003 being used) and then when the email is sent have the contents
of the email added as a note to the support call in the database.

I created a dummy VB project using VB6 to make sure I could do what I wanted
in this regard. I added a button to the form that creates a new instance of a
class and then calls the SendSupportMail() method. The contents of this class
are included below, however neither the ItemSend nor the Send events are
fired even though the emails themselves are successfully sent. Breakpoints
that I've added on the Msgbox statements are never reached.

What am I doing wrong?

----------------------------------------------------------------------------------------

Private WithEvents objApp As Outlook.Application
Private WithEvents objMail As Outlook.MailItem

Private Sub Class_Initialize()
Set objApp = Outlook.Application
End Sub

Private Sub objApp_ItemSend(ByVal Item As Object, Cancel As Boolean)
MsgBox "Test"
End Sub

Public Sub SendSupportMail()
Set objMail = objApp.CreateItem(olMailItem)
objMail.Display
End Sub

Private Sub objMail_Send(Cancel As Boolean)
MsgBox "Test2"
End Sub
 
So your button click instantiates an instance of this class and calls the
SendSupportMail() method and then the button click event handler ends?

If that's the case, the class instance is going out of scope at the point
where the click event handler ends. So at that time the Send() and
ItemSend() event handlers have already been released and can't fire.

How is this button created? Is it created in your VB6 code? Where does that
code reside? If it's in another part of your VB6 code then add a reference
to the class you create at a module level that will retain scope so the
event handlers continue to fire. Something like this:

' at module level in module where button click handler resides
Dim myClass As WhateverTheClassIsCalled

' in button click handler
Set myClass = New WhateverTheClassIsCalled

That way as long as the module has scope the class will have scope and the
events should fire.
 
D'oh! I'd made the stupid mistake of having the class variable declared
inside the event handler for the button. Moving that declaration outside the
click event handler appears to have fixed the problem. Now both message boxes
are appearing.

Thank you for your time.
 
Back
Top