itemadd not firing

M

mlafarlett

I've seen this post numerous times but can't seem to derive the
resolution...I've written a very small test app (vb.net code below) to
demonstrate. My end goal is to have an app to continually monitor the
inbox for new entries and process them according to subject , however,
the event never fires. Thanks in advance, Michael

Imports Outlook = Microsoft.Office.Interop.Outlook

Module subMain
Dim WithEvents oRcvdItems As Outlook.Items
Sub main()

Dim oOL As Outlook.Application
Dim oNS As Outlook.NameSpace

oOL = CreateObject("Outlook.Application")
oNS = oOL.GetNamespace("MAPI")
oRcvdItems =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Items

'Sleep forever so we can wait on stuff to show up ( IS THERE A BETTER
WAY TO DO 'THIS?)

SleepForAwhile:
System.Threading.Thread.Sleep(15000)
GoTo SleepForAwhile

oOL = Nothing
oNS = Nothing
oRcvdItems = Nothing
End Sub

Private Sub oRcvdItems_ItemAdd(ByVal Item As Object)
Debug.WriteLine(Item.subject)
End Sub

End Module
 
G

Guest

The ItemAdd event won't fire if a large number of messages are
added/delivered at the same time. Otherwise, you can write an Exchange Event
Sink to intercept the messages before they are even delivered to your Inbox.

OL2002: ItemAdd Event Doesn't Run in Some Scenarios:
http://support.microsoft.com/default.aspx?scid=kb;en-us;290653

Otherwise, if you do use Exchange the NewMailEx event in Outlook 2003
provides a handle to the EntryID values of all newly receieved e-mails that
you can call individually.
 
M

mlafarlett

it will not fire at all...i'm using on my on inbox and i have to send
myself an email just to test...its not like its overwhelmed with
incoming messages....also tried the sample code of the NewMailEx and it
does not fire. It can be standalone code (vb.net) can't it? doesn't
have to be vba / add-in does it? I'm sure once i solve one, either
ItemAdd or NewMailEx, the other will work as well..at least i suspect
so.
 
G

Guest

As long as you reference the Items collection properly using With Events for
the folder you want to monitor, it doesn't matter whether you use VBA, VB.NET
or a COM Add-In.

Is your oOL object variable valid when you retrieve it? If not, use
GetObject instead to work with an existing instance of Outlook instead of
creating a new one.

Also try commenting out your timer code, because it may be dereferencing the
oRcvdItems collection inadvertently.
 
M

mlafarlett

The oOL object is valid and I do not know of another way to keep from
falling thru 'sub main' and the program terminating other than using
the sleep..also i've had the NewMail event fires fine with the sleep
code in there.
 
G

Guest

I think I know what your concerns are. All you need to do is ensure that you
"hook up" Outlook the right way at the beginning and at the end of where it
is appropriate in your solution.

Take a Windows Form as an example, with one button that initializes the
integration with Outlook (Button1), and the other button which terminates it
(Button2):

Public Class Form1
Inherits System.Windows.Forms.Form

Dim myFolderMonitor As FolderMonitor

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
myFolderMonitor = New FolderMonitor
End Sub

Private Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
myFolderMonitor = Nothing
Me.Close()
End Sub
End Class


It's always best to encapsulate this integration with a class, hence the
FolderMonitor:


Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class FolderMonitor
Private WithEvents oRcvdItems As Outlook.Items
Private objNS As Outlook.NameSpace
Private objOL As Outlook.Application
Private blnCloseWhenDone As Boolean

Public Sub New()
Dim objInbox As Outlook.MAPIFolder
Try
objOL = GetObject("", "Outlook.Application")
Catch ex As Exception
objOL = New Outlook.Application
blnCloseWhenDone = True
End Try

objNS = objOL.GetNamespace("MAPI")
objInbox =
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
oRcvdItems = objInbox.Items
End Sub

Private Sub oRcvdItems_ItemAdd(ByVal Item As Object) Handles
oRcvdItems.ItemAdd
Debug.WriteLine(Item.Subject)
End Sub

Protected Overrides Sub Finalize()
If blnCloseWhenDone = True Then
objOL.Quit()
End If

MyBase.Finalize()
End Sub
End Class


As long as an instance of the FolderMonitor class is alive, the oRcvdItems
Collection will continue to fire when new items are added to the folder.
When your app is done, dereference the class and it will tear down the
Outlook integration as appropriate.
 
M

mlafarlett

well...i got it work by examining what you sent me...what i was missing
was the Handles
oRcvdItems.ItemAdd ....my chore now is to find a better alternative to
sleep..although i'm not sure there's really anything wrong with it
since it does not keep my event from firing. The stuff you sent me has
a form which basically keeps things alive for you...my stuff does not
have a form....if you have any ideas off the top of your head i'd
appreciate it , otherwise, you've helped my pasted what i was really
trying to solve and i appreciate it.

Thanks,
Michael
 

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