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
--
Trefor
--
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" <Tre...@home.com> wrote in message news:E67BFB73-C76D-4688...@microsoft.com...
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
--
Trefor
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" <Tre...@home.com> wrote in message news:D91F0EFF-B98D-45E8...@microsoft.com...
>
> "Sue Mosher [MVP-Outlook]" wrote:
>
>> 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.
>>
>>
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
.
.
--
Trefor
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" <Tre...@home.com> wrote in message news:F9ACD8AA-EA31-4627...@microsoft.com...
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?
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" <Tre...@home.com> wrote in message news:F2E1E67C-5F65-4E4F...@microsoft.com...
>>
>>
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
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
"Trefor" <Tre...@home.com> wrote in message news:04E03D2D-3D46-4F0D...@microsoft.com...
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.