Problem using NewMailEx

T

Trefor

We have several users sharing a mailbox using the "Open these additional
mailboxes" option. Trouble is even if the VBA code is setup using this
configuration the procedure will not fire. If I create a new profile and
start outlook directly to this mailbox the sub routine fires just fine.

Does NewMailEx only work when you open the mailbox directly? Or am I doing
something wrong or is there a better way to do this?

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)

.....stuff

end sub
 
S

Sue Mosher [MVP-Outlook]

Correct: NewMailEx fires only for the user's own mail accounts, not for new mail delivered to other mailboxes opened as secondary mailboxes. The event to use for folders from other mailboxes would be MAPIFolder.Items.Add.
 
T

Trefor

Sue,

Thankyou for the feedback. I have tried to change the code to match your
suggestion, but being a newbie to Outlook VBA I seem to be struggling.

I want to monitor a specific folder not just the Inbox. Below is my code,
can you let me know what I have done wrong. Also all my code is in
ThisOutlookSession, is this a problem?


Option Explicit
Private WithEvents InputFolder As Items

Private Sub Application_Startup()
Dim olNS As NameSpace
Dim InputFolder As Outlook.MAPIFolder
Dim ProcessedFolder As Outlook.MAPIFolder
Dim olMail As Items

Set olNS = Outlook.Application.GetNamespace("MAPI")

' Get reference to folder in user’s Mailbox for Input
Set InputFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification")

' Get reference to folder in user’s Mailbox for Output (or Processed)
Set ProcessedFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification Processed")

' Get reference to items in folder
Set olMail = InputFolder.Items

End Sub


Private Sub InputFolder_ItemAdd(ByVal Item As Object)

.....stuff

End Sub
 
S

Sue Mosher [MVP-Outlook]

Putting your code in ThisOutlookSession was the right thing to do.

The problem is that you have duplicate declarations for InputFolder. Once as Items and once as MAPIFolder. You need to use two separate objects. It will be easiest to change the name of the MAPIFolder object. You will then need to add a statement to instantiate the Items object (InputFolder).

Also, there seems to be no purpose for the local olMail object in the Application_Startup procedure.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Trefor said:
Sue,

Thankyou for the feedback. I have tried to change the code to match your
suggestion, but being a newbie to Outlook VBA I seem to be struggling.

I want to monitor a specific folder not just the Inbox. Below is my code,
can you let me know what I have done wrong. Also all my code is in
ThisOutlookSession, is this a problem?


Option Explicit
Private WithEvents InputFolder As Items

Private Sub Application_Startup()
Dim olNS As NameSpace
Dim InputFolder As Outlook.MAPIFolder
Dim ProcessedFolder As Outlook.MAPIFolder
Dim olMail As Items

Set olNS = Outlook.Application.GetNamespace("MAPI")

' Get reference to folder in user’s Mailbox for Input
Set InputFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification")

' Get reference to folder in user’s Mailbox for Output (or Processed)
Set ProcessedFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification Processed")

' Get reference to items in folder
Set olMail = InputFolder.Items

End Sub


Private Sub InputFolder_ItemAdd(ByVal Item As Object)

....stuff

End Sub
 
T

Trefor

Sue,

Sorry you lost me there.

"Once as Items..."
which line?

"Once as MAPIFolder."
Dim InputFolder As Outlook.MAPIFolder
Set InputFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification")

The following in start up
Dim olMail As Items
Set olMail = InputFolder.Items

are related to

Private Sub InputFolder_ItemAdd(ByVal Item As Object)

Dim MessageText As String
Dim ItemReference As Integer

For ItemReference = olMail.Count To 1 Step -1

On Error Resume Next
MessageText = olMail(ItemReference).Body
If Err <> 0 Then MessageText = ""
On Error GoTo 0
..
..
olMail(ItemReference).UnRead = False
olMail(ItemReference).Move ProcessedFolder
..
..
 
S

Sue Mosher [MVP-Outlook]

"Once as Items..."
which line?

In the declarations section:

Private WithEvents InputFolder As Items

In Application_Startup:

Dim InputFolder As Outlook.MAPIFolder

In your Items.ItemAdd event handler, you should be processing Item, the new item added to the folder, e.g. instead of

MessageText = olMail(ItemReference).Body

you'd get rid of the For loop and simply use

MessageText = Item.Body
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Trefor said:
Sue,

Sorry you lost me there.

"Once as Items..."
which line?

"Once as MAPIFolder."
Dim InputFolder As Outlook.MAPIFolder
Set InputFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification")

The following in start up
Dim olMail As Items
Set olMail = InputFolder.Items

are related to

Private Sub InputFolder_ItemAdd(ByVal Item As Object)

Dim MessageText As String
Dim ItemReference As Integer

For ItemReference = olMail.Count To 1 Step -1

On Error Resume Next
MessageText = olMail(ItemReference).Body
If Err <> 0 Then MessageText = ""
On Error GoTo 0
.
.
olMail(ItemReference).UnRead = False
olMail(ItemReference).Move ProcessedFolder
.
.
 
T

Trefor

Sue,

I original wrote the code to be fired manually, then reading comments like
"more than 16 emails will not trigger this code" it seemed to make sense to
let the code clean up whatever it found in the folder.

I obviously still don’t understand these commands.

I assumed this meant look for new Items (emails) in a particular folder
(InputFolder)

Private WithEvents InputFolder As Items

This is telling Outlook that InputFolder is an MAPIFolder, so this is about
the folder not the Item/email?

Dim InputFolder As Outlook.MAPIFolder


Are you saying I can’t clean up all emails found in the folder? Only the one
that has just arrived? I am really keen at this stage to have code that will
work on whatever it finds in this directory, is there a better way of doing
this?
 
S

Sue Mosher [MVP-Outlook]

If you want to process all unprocessed items in the folder when Items.ItemAdd fires, that's fine. But don't do it by processing *every* item. That's very inefficient. Use Items.Find or Items.Restrict to filter the Items collection so that you process only items that need processing.

The problem with InputFolder is two-fold: (a) You're never instantiating the module-level object that is supposed to fire the events. You're only instantiating the variable that is local to the Application_Startup event handler. (b) Even if you take out the Dim statement in Application_Startup, you'd still have a problem, because the statement that instantiates InputFolder returns a MAPIFolder object, not an Items collection. Therefore, you need to take out that extraneous Dim statement and fix the Set InputFolder statement so that it matches the Private WithEvents declaration.
--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


Trefor said:
Sue,

I original wrote the code to be fired manually, then reading comments like
"more than 16 emails will not trigger this code" it seemed to make sense to
let the code clean up whatever it found in the folder.

I obviously still don’t understand these commands.

I assumed this meant look for new Items (emails) in a particular folder
(InputFolder)

Private WithEvents InputFolder As Items

This is telling Outlook that InputFolder is an MAPIFolder, so this is about
the folder not the Item/email?

Dim InputFolder As Outlook.MAPIFolder


Are you saying I can’t clean up all emails found in the folder? Only the one
that has just arrived? I am really keen at this stage to have code that will
work on whatever it finds in this directory, is there a better way of doing
this?
 
T

Trefor

Items.Find – Can this be used to find text in the body of an email? I have
already narrowed the Items I am looking at down to as few as possible by
using a rule. The only way I know now which emails I want (or not) is by
check to see if there are certain words in the body text.

To do this I have used this:

For ItemReference = olMail.Count To 1 Step -1

MessageText = olMail(ItemReference).Body

If InStr(MessageText, "my text") > 0 Then
‘ Then Parse MessageText to get the various bits of info I need

Sue as for the rest of your comments I don’t mean to be rude, but “you speak
with forked tongue ;)â€. I am not a professional developer and have never done
any coding in Outlook and you clearly are one of the guru’s in this area.

I have made some changes that sort of work and have include the full sub
routines below:


I realize NewMailEx is looking at my mailbox and not the shared mailbox, but
it is a way of periodically checking the shared mailbox. I feel you cringing
already ;)

Private Sub Application_NewMailEx(ByVal EntryIDCollection As String)
Call ProcessEmails
End Sub

Private Sub Application_Startup()
Call ProcessEmails
End Sub

Private Sub ProcessEmails()

Dim DeletedFolder As Outlook.MAPIFolder
Dim olNS As NameSpace
Dim InputFolder As Outlook.MAPIFolder
Dim ProcessedFolder As Outlook.MAPIFolder


Set olNS = Outlook.Application.GetNamespace("MAPI")
Set InputFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification")
Set ProcessedFolder = olNS.Folders("Mailbox - Partners").Folders("Order
Notification Processed")
Set DeletedFolder = olNS.Folders("Mailbox - Partners").Folders("Deleted
Items")

Dim MessageText As String
Dim ItemReference As Integer
Dim olMail As Items

' Get reference to items in folder
Set olMail = InputFolder.Items


For ItemReference = olMail.Count To 1 Step -1

On Error Resume Next
MessageText = olMail(ItemReference).Body
If Err <> 0 Then MessageText = ""
On Error GoTo 0

If InStr(MessageText, "my text ") > 0 Then
' Parse this email.
Call ParseEmail(MessageText, GoodReturn)
BookingElement(18) = olMail(ItemReference).ReceivedTime

If GoodReturn Then
Call WriteTransactionFile(GoodReturn)
If GoodReturn Then
olMail(ItemReference).UnRead = False
olMail(ItemReference).Move ProcessedFolder
On Error Resume Next
olMail(ItemReference).UnRead = False ' Per suggestion
from Ken Slovak re maybe having to set this twice.
On Error GoTo 0
End If
End If
Else
olMail(ItemReference).Move DeletedFolder
End If
Next ItemReference

Set InputFolder = Nothing
Set ProcessedFolder = Nothing
Set olNS = Nothing
Set olMail = Nothing
End Sub
 
S

Sue Mosher [MVP-Outlook]

Items.Find – Can this be used to find text in the body of an email?

No, but Restrict can. However, if you already have a rule moving items to be processed into a particular folder and code moving them out after they're processed, that may sufficient if there aren't too many items. Let me know if you want to try using Restrict.
I realize NewMailEx is looking at my mailbox and not the shared mailbox, but
it is a way of periodically checking the shared mailbox. I feel you cringing
already ;)

Yes, that makes me cringe, because it means that if you don't receive any messages in your own mailbox for a long period of time, those in the other mailbox will go unprocessed. If you want to pursue using the Items.ItemAdd event, let me know, as it looks like we'll need to go over some VBA basics like working with event-enabled variables that aren't related specifically to Outlook code.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54
 
T

Trefor

Sue,

Ok so I will leave the find/filtering as is, thankyou for your feedback on
this.

So this leaves the trigger, yes I know this was not ideal but with my
limited knowledge it was all I could think of for know as a work around to
get something happening. Also this code will create text files that I will be
processing in Excel and I still have the Excel code to finish off so I am not
locked into a solution for this code yet. I am keen to have a more
elegant/appropriate firing mechanism, but am just struggling being new to
Outlook and even the use of objects in general. Any help would be very much
appreciated.
 

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