Capture Outlook Items' events in VBA?

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
 
M

Michael Bauer

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

John Riddle

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.
 
J

John Riddle

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.
 
K

Ken Slovak - [MVP - Outlook]

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.
 
J

John Riddle

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.
 
J

John Riddle

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.
 
M

Michael Bauer

John,

in that case use the SelectionChange event and check if
ActiveExplorer.IsPaneVisible(olPreview)=True.
 
K

Ken Slovak - [MVP - Outlook]

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.
 
J

John Riddle

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.
 
J

John Riddle

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.
 
K

Ken Slovak - [MVP - Outlook]

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.
 
J

John Riddle

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.
 

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