Link to home
Start Free TrialLog in
Avatar of jdannemann
jdannemannFlag for United States of America

asked on

How to set up Outlook Events in .NET?

This is perplexing. How do I get the Microsoft Outlook Application Events to fire in VB.NET code?
Imports Outlook = Microsoft.Office.Interop.Outlook
Public Class OutlookInterface

    'Office XP PIA's required for use of events
    'http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-A6B4-01FEBA508E52&displaylang=en

    Public WithEvents Application As Outlook.Application

    Sub New()
        'If Outlook is already running then connect to it, else
        'start Outlook
        If Object.Equals(Nothing, GetObject("", "Outlook._Application")) Then
            Application = New Outlook.Application
        Else
            Application = GetObject("", "Outlook._Application")
        End If
    End Sub
    Private Sub Application_NewMail() Handles Application.NewMail
        MsgBox("it worked")
    End Sub
End Class

Open in new window

Avatar of x77
x77
Flag of Spain image

http://msdn.microsoft.com/en-us/library/bb147645.aspx

This Microsoft Visual Basic/Visual Basic for Applications (VBA) example displays the Inbox folder when a new e-mail message arrives. The sample code must be placed in a class module, and the Initialize_handler routine must be called before the event procedure can be called by Microsoft Outlook.

---

If Outlook is open, then you calls GetObject twice.  It Returns an Interop Object, next Other.

Application = GetObject("", "Outlook.Application")
If Application is nothing then  = Application = New Outlook.Application
Avatar of jdannemann

ASKER

@x77 - Thanks, so I guess your recommendation is to just program it in the Outlook VBA Editor? I could do that, but its not really the problem, and I already know how to set up the events in VBA.

( BTW, a little advice on that. In VBA, if you want to skip having to substantiate an Outlook Application object with events so you can access the events, then install the MAPI Security Labs Outlook Add-in first, and you can skip those steps. Essentially, this allows you to simply call the Application object, or make event triggered sub-routines in the same manner as one might do when programming Excel in the VBA editor. )

The problem I'm having is in VB.NET and getting Outlook events to fire when they should. The code I posted is .NET code, not VBA. I'm hoping to figure out a way to get the application events such as NewMail to fire inside a tray application I am designing.
ASKER CERTIFIED SOLUTION
Avatar of jdannemann
jdannemann
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial