• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

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

0
jdannemann
Asked:
jdannemann
  • 2
1 Solution
 
x77Commented:
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
0
 
jdannemannAuthor Commented:
@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.
0
 
jdannemannAuthor Commented:
Cool. I figured this out on my own, so for anyone who runs into this problem in the future... hopefully I can save you a bit of trouble.

The problem I was encountering had little to do with my code, though my code was incomplete. I had an object declared in a module for the Outlookinterface class I designed. It stated:

Public Outlook As OutlookInterface = New Outlookinterface

Pretty straightforward right? Well unfortunately, substantiating the object this way is what caused the problem in the first place. So instead, I declared the public object, but substantiated it while running the Form1_Load procedure like so:

Private Sub Console_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Outlook = New OutlookInterface
 End Sub

Last, the each event has to be attached to an Event Handler. Example attached in the code below:

Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class OutlookInterface

    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

        'Attach Event Handlers
        AddHandler Application.NewMail, AddressOf NewMail
        AddHandler Application.NewMailex, AddressOf Newmailex
        AddHandler Application.ItemSend, AddressOf ItemSend

    End Sub

    Private Sub NewMail()
        MsgBox("It Worked!")
    End Sub

    Private Sub Newmailex()
        MsgBox("It Worked!")
    End Sub

    Private Sub ItemSend()
        MsgBox("It Worked!")
    End Sub

End Class

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now