Consulting

Results 1 to 6 of 6

Thread: Solved: Catch Opening Item?

  1. #1
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location

    Solved: Catch Opening Item?

    Is there anyway of writing VBA in VbaProject to react to a contact being opened and closed?

    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

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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: [vba]
    '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

    [/vba]

    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: [vba]
    '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

    [/vba]

    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..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location
    Joost, you are a genius!!!!

    Thank you again.

    Peter H

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Peter,

    Thank you, you're most welcome!

    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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #5
    VBAX Regular
    Joined
    Jun 2006
    Posts
    20
    Location
    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

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by PMHCSLtd
    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)
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •