Capture Outlook Items' events in VBA?

  • Thread starter Thread starter John Riddle
  • Start date Start date
J

John Riddle

Hello,

I'm trying to replace the code behind my Outlook forms with VBA code, but am
having trouble capturing the events. Here's what I've got so far:

Public WithEvents VBAInspectors As Inspectors
Public WithEvents VBAInspector As Inspector
Public WithEvents VBAContact As ContactItem

Private Sub Initialize_Handler()
Set VBAInspectors = Application.Inspectors
End Sub

Private Sub Application_Startup()
Initialize_Handler
End Sub

Private Sub VBAContact_Open(Cancel As Boolean)
MsgBox "Item Opened"
End Sub

Private Sub VBAInspector_Activate()
Set objItem = VBAInspector.CurrentItem
If objItem.MessageClass = "IPM.Contact" Then
Set VBAContact = VBAInspector.CurrentItem
MsgBox VBAContact.FullName
Else
Set VBAContact = Nothing
End If
End Sub

Private Sub VBAInspectors_NewInspector(ByVal Inspector As Inspector)
Set VBAInspector = Inspector
End Sub

The "MsgBox VBAContact.FullName executes fine, but the "Item Opened" message
in the VBAContact_Open event does not fire.

Any suggestions?

Thanks,

John
 
John, in VBAInspector_Activate it´s too late, the item is opened
already. Use the NewInspector event instead.
 
Okay. I tried that already but when doing it that way, I have no way to keep
track of multiple open contacts. That's to say, switching between one contact
and another does not fire the VBAInspector_Activate event. How can I work with
several contacts switching between them and still fire the events...
propertychange, custompropertychange, etc.?

I have about 1000 lines of code behind my custom contact form and I need to
move this to a VBA module for the moment and then once I verify that
everything is working properly, I'll work on converting it to a Com Addin.

Thanks,

John
John, in VBAInspector_Activate it´s too late, the item is opened
already. Use the NewInspector event instead.
 
Ken,

Thanks! That worked great. Now I have an issue where events are firing
multiple times (once for each time the item as been opened).

For example, if I put the statement

MsgBox "I'm open"

into the m_objContact_Open() event, it fires once the first time. The next
time I open the item, I get two message boxes, the third time I get three of
them and so on...

Any idea what I might have done wrong?

Thanks,

John

John, to handle multiple Inspectors or Explorers you need a wrapper class.
That class handles all the events for the Inspector or Explorer and is put
into a collection to keep the reference alive. See
http://www.slovaktech.com/code_samples.htm#InspectorWrapper for an example
of a VB 6 Inspector wrapper. The ItemsCB VB 6 sample on the Resources page
at www.microeye.com has an example of an Explorer wrapper in it.
 
It sounds like you aren't using unique Key properties for each item in the
wrapper class collection. Just like with custom command bar buttons where a
unique Tag property is required to prevent a button click from firing in
each Inspector that is open you need to use unique Key properties for an
Inspector wrapper.

Also, make sure each Inspector wrapper class is released and removed from
the Inspectors wrapper collection when the Inspector is closed.
 
After going through the code very closely to see what was happening, I figured
out that I had named my regular module "OutlInsp" instead of "basOutlInsp"
like your instructions stated. I simply changed the line:

basOutlInsp.KillInsp m_intID, Me

To:

KillInsp m_intID, Me

and everything works great now.

Thanks!

John


John, to handle multiple Inspectors or Explorers you need a wrapper class.
That class handles all the events for the Inspector or Explorer and is put
into a collection to keep the reference alive. See
http://www.slovaktech.com/code_samples.htm#InspectorWrapper for an example
of a VB 6 Inspector wrapper. The ItemsCB VB 6 sample on the Resources page
at www.microeye.com has an example of an Explorer wrapper in it.
 
Ken,

I mostly have everything working now. Except in my custom form, the Item_Read
event would fire when the item was selected in a view. However, now that I've
ported my code over to VBA, the Read event only fires when the item is
double-clicked (opened). How can I get a read event to fire from VBA when an
item in a folder is selected and previewed?

Thanks for all your help!

John


It sounds like you aren't using unique Key properties for each item in the
wrapper class collection. Just like with custom command bar buttons where a
unique Tag property is required to prevent a button click from firing in
each Inspector that is open you need to use unique Key properties for an
Inspector wrapper.

Also, make sure each Inspector wrapper class is released and removed from
the Inspectors wrapper collection when the Inspector is closed.
 
John,

in that case use the SelectionChange event and check if
ActiveExplorer.IsPaneVisible(olPreview)=True.
 
As Michael suggested, you could use SelectionChange of an Explorer object.
Item_Read won't fire for an Inspector wrapper class since the item hasn't
been opened and therefore there is no Inspector for it.

A tricky workaround that actually handles the original Item_Read even would
be to check the Explorer's Selection collection and each time it changed
check the count. If it's 1 then instantiate a MailItem (or other type
depending on the Class of the selected object) and declare for that MailItem
WithEvents declarations for any events you want to handle such as Read.
 
Thanks guys. That did the trick for me.

John

As Michael suggested, you could use SelectionChange of an Explorer object.
Item_Read won't fire for an Inspector wrapper class since the item hasn't
been opened and therefore there is no Inspector for it.

A tricky workaround that actually handles the original Item_Read even would
be to check the Explorer's Selection collection and each time it changed
check the count. If it's 1 then instantiate a MailItem (or other type
depending on the Class of the selected object) and declare for that MailItem
WithEvents declarations for any events you want to handle such as Read.
 
Ken,

Now that I've got everything working in VBA, I'm working on porting the code
to a ComAddin developed using VSTO for Outlook. I'm getting a compile error on
the clsInspWrap class when defining the properties that methods Get/Set/Let
are no longer supported. Please ues the correct syntax for defining
properties.

Any ideas how to modify the code sample to be used with .NET?

Thanks,

John


As Michael suggested, you could use SelectionChange of an Explorer object.
Item_Read won't fire for an Inspector wrapper class since the item hasn't
been opened and therefore there is no Inspector for it.

A tricky workaround that actually handles the original Item_Read even would
be to check the Explorer's Selection collection and each time it changed
check the count. If it's 1 then instantiate a MailItem (or other type
depending on the Class of the selected object) and declare for that MailItem
WithEvents declarations for any events you want to handle such as Read.
 
Beats me, I don't use .NET code for Outlook addins at all.

Sue's been playing with VSTO, start a new thread with VSTO in the subject
and she'll probably spot it.
 
I'll do that, thank!

John

Beats me, I don't use .NET code for Outlook addins at all.

Sue's been playing with VSTO, start a new thread with VSTO in the subject
and she'll probably spot it.
 
Cannot get event handler to pick up events

I am trying to implement Slovak's code, but I can't it to capture the events. Here is what I have in the "ThisOutlookSession"


Dim clsInspWrap As clsInspWrap

Private Sub Application_Quit()
Set clsInspWrap = Nothing
End Sub

Private Sub Application_Startup()
Set clsInspWrap = New clsInspWrap

End Sub


-Tim.
 
I now have another issue. If I have one email open, open a second email, try to close the second email, the close event won't stop firing.

Any thoughts?

-Tim.
 
Back
Top