PDA

View Full Version : Solved: Catch Opening Item?



PMHCSLtd
07-17-2006, 12:01 PM
Is there anyway of writing VBA in VbaProject to react to a contact being opened and closed? :banghead:

What we are trying to achieve is for an action to be taken depending on the Contact category being opened or closed. I appreciate we could use VB Script in a custome form but would prefer to run from VBA.

Peter H

MOS MASTER
07-17-2006, 01:01 PM
Hi Peter, :)

Ok this is doable but tricky so read this carefully! This is bases on a kb items of mine to put a button on the opened E-mail.

1. First create a new classmodule in the vbe and call it: clsOpenEvent

2. second paste the below code in the class module:
'Code in Classmodule: clsOpenEvent

Option Explicit
Dim WithEvents oAppInspectors As Outlook.Inspectors
Dim WithEvents oMailInspector As Outlook.Inspector
Dim WithEvents oContactItem As Outlook.ContactItem

'Class Initialize
Private Sub Class_Initialize()
'oAppInspectors Returns a handle to the Inspectors collection
Set oAppInspectors = Application.Inspectors
End Sub

Private Sub oAppInspectors_NewInspector(ByVal Inspector As Inspector)
'Event gets triggered every time a Window or Item is opened in Outlook Interface
'Like: E-mail, Contacts, Tasks
If Inspector.CurrentItem.Class <> olContact Then
'Only deal with Contact Items...else exit
Exit Sub
End If

'Set a reference to the e-mail to trap the Open event
Set oContactItem = Inspector.CurrentItem
Set oMailInspector = Inspector
End Sub

Private Sub oContactItem_Open(Cancel As Boolean)
'Event gets triggerd if oContactItem is Opened
'do what you like here
MsgBox "you opened a contactitem from: " & vbCr & oContactItem.FirstName
End Sub

Private Sub oContactItem_Close(Cancel As Boolean)
'Event gets triggered if oOpenMail is Closed!
MsgBox "you Closed a contactitem from: " & vbCr & oContactItem.FirstName
Set oContactItem = Nothing
End Sub

'Class Terminate
Private Sub Class_Terminate()
'Clean up Globals
Set oAppInspectors = Nothing
Set oContactItem = Nothing
End Sub



3. Look in the projectexplorer on the left and double-click on the classmodule: ThisOutlookSession

Paste this code above all other code present (if any) in that module:
'Code in ThisOutlookSession

Option Explicit
Dim TrapInspector As clsOpenEvent

Private Sub Application_Quit()
'Event gets triggered when you quit Outlook
'Clean up
Set TrapInspector = Nothing
End Sub

Private Sub Application_Startup()
'Event gets triggered when you start Outlook
'Initialize class "clsInspector"
Set TrapInspector = New clsOpenEvent
End Sub



4. Now save your project and close Outlook
5. Restart Outlook when you open and close a contact item you receive a msg

You're done!

HTH.. :whistle:

PMHCSLtd
07-17-2006, 01:14 PM
Joost, you are a genius!!!!

Thank you again.

Peter H

MOS MASTER
07-17-2006, 01:15 PM
Hi Peter, :hi:

Thank you, you're most welcome! :thumb

Remember that code is very powerfull and flexible to adopt to a whole other range of event driven stuff you wanna do in Outlook. So have fun with it!

PMHCSLtd
07-17-2006, 01:20 PM
Interestingly the code works on Contacts every time one is opened. If more than one is open then the first to close generates the mesage box but subsequebt closures do not generate the message.

I shall have a play now that you have given me the start!

Pete H

MOS MASTER
07-17-2006, 01:25 PM
Interestingly the code works on Contacts every time one is opened. If more than one is open then the first to close generates the mesage box but subsequebt closures do not generate the message.

Hi Peter, :)

To me that sounds live pure logic. That's why I put lots of comments in there. if you read the code and understand it. Then you'll be able to explain youreself why things happen the way you explain them.

If it becomes a problem I will help of course but for now it's more interesting for you to understand why things happen.. :*) (So yeah play with it as much as you can)