Problem using NewMailEx

Discussion in 'Microsoft Outlook VBA Programming' started by Trefor, May 29, 2008.

  1. Trefor

    Trefor Guest

    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

    --
    Trefor
     
    Trefor, May 29, 2008
    #1
    1. Advertisements

  2. 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.

    --
    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" <> wrote in message news:...
    > 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
    >
    > --
    > Trefor
     
    Sue Mosher [MVP-Outlook], May 29, 2008
    #2
    1. Advertisements

  3. Trefor

    Trefor Guest

    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




    --
    Trefor


    "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.
    >
    > --
    > 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" <> wrote in message news:...
    > > 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
    > >
    > > --
    > > Trefor

    >
     
    Trefor, May 29, 2008
    #3
  4. 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" <> wrote in message news:D...
    > 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
    >


    >
    > "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.
    >>
    >>
    >> "Trefor" <> wrote in message news:...
    >> > 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
    >> >
    >> > --
    >> > Trefor

    >>
     
    Sue Mosher [MVP-Outlook], May 29, 2008
    #4
  5. Trefor

    Trefor Guest

    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
    ..
    ..


    --
    Trefor


    "Sue Mosher [MVP-Outlook]" wrote:

    > 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" <> wrote in message news:D...
    > > 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
    > >

    >
    > >
    > > "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.
    > >>
    > >>
    > >> "Trefor" <> wrote in message news:...
    > >> > 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
    > >> >
    > >> > --
    > >> > Trefor
    > >>

    >
     
    Trefor, May 29, 2008
    #5
  6. > "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" <> wrote in message news:...
    > 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
    > .
    > .
    >
    >
    > --
    > Trefor
    >
    >
    > "Sue Mosher [MVP-Outlook]" wrote:
    >
    >> 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" <> wrote in message news:D...
    >> > 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
    >> >

    >>
    >> >
    >> > "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.
    >> >>
    >> >>
    >> >> "Trefor" <> wrote in message news:...
    >> >> > 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
    >> >> >
    >> >> > --
    >> >> > Trefor
    >> >>

    >>
     
    Sue Mosher [MVP-Outlook], May 29, 2008
    #6
  7. Trefor

    Trefor Guest

    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?

    --
    Trefor


    "Sue Mosher [MVP-Outlook]" wrote:

    > > "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" <> wrote in message news:...
    > > 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
    > > .
    > > .
    > >
    > >
    > > --
    > > Trefor
    > >
    > >
    > > "Sue Mosher [MVP-Outlook]" wrote:
    > >
    > >> 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" <> wrote in message news:D...
    > >> > 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
    > >> >
    > >>
    > >> >
    > >> > "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.
    > >> >>
    > >> >>
    > >> >> "Trefor" <> wrote in message news:...
    > >> >> > 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
    > >> >> >
    > >> >> > --
    > >> >> > Trefor
    > >> >>
    > >>

    >
     
    Trefor, May 30, 2008
    #7
  8. 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" <> wrote in message news:...
    > 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?
    >
    > --
    > Trefor
    >
    >
    > "Sue Mosher [MVP-Outlook]" wrote:
    >
    >> > "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


    >>
    >>
    >> "Trefor" <> wrote in message news:...
    >> > 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
    >> > .
    >> > .
    >> >
    >> >
    >> > --
    >> > Trefor
    >> >
    >> >
    >> > "Sue Mosher [MVP-Outlook]" wrote:
    >> >
    >> >> 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" <> wrote in message news:D...
    >> >> > 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
    >> >> >
    >> >>
    >> >> >
    >> >> > "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.
    >> >> >>
    >> >> >>
    >> >> >> "Trefor" <> wrote in message news:...
    >> >> >> > 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
    >> >> >> >
    >> >> >> > --
    >> >> >> > Trefor
    >> >> >>
    >> >>

    >>
     
    Sue Mosher [MVP-Outlook], May 30, 2008
    #8
  9. Trefor

    Trefor Guest

    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


    --
    Trefor


    "Sue Mosher [MVP-Outlook]" wrote:

    > 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" <> wrote in message news:...
    > > 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?
    > >
    > > --
    > > Trefor
    > >
    > >
    > > "Sue Mosher [MVP-Outlook]" wrote:
    > >
    > >> > "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

    >
    > >>
    > >>
    > >> "Trefor" <> wrote in message news:...
    > >> > 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
    > >> > .
    > >> > .
    > >> >
    > >> >
    > >> > --
    > >> > Trefor
    > >> >
    > >> >
    > >> > "Sue Mosher [MVP-Outlook]" wrote:
    > >> >
    > >> >> 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" <> wrote in message news:D...
    > >> >> > 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
    > >> >> >
    > >> >>
    > >> >> >
    > >> >> > "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.
    > >> >> >>
    > >> >> >>
    > >> >> >> "Trefor" <> wrote in message news:...
    > >> >> >> > 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
    > >> >> >> >
    > >> >> >> > --
    > >> >> >> > Trefor
    > >> >> >>
    > >> >>
    > >>

    >
     
    Trefor, May 30, 2008
    #9
  10. > 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


    "Trefor" <> wrote in message news:...
    > 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
    >
    >
    > --
    > Trefor
    >
    >
    > "Sue Mosher [MVP-Outlook]" wrote:
    >
    >> 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.
    >>
    >> "Trefor" <> wrote in message news:...
    >> > 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?
    >> >
    >> > --
    >> > Trefor
    >> >
    >> >
    >> > "Sue Mosher [MVP-Outlook]" wrote:
    >> >
    >> >> > "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

    >>
    >> >>
    >> >>
    >> >> "Trefor" <> wrote in message news:...
    >> >> > 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
    >> >> > .
    >> >> > .
    >> >> >
    >> >> >
    >> >> > --
    >> >> > Trefor
    >> >> >
    >> >> >
    >> >> > "Sue Mosher [MVP-Outlook]" wrote:
    >> >> >
    >> >> >> 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" <> wrote in message news:D...
    >> >> >> > 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
    >> >> >> >
    >> >> >>
    >> >> >> >
    >> >> >> > "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.
    >> >> >> >>
    >> >> >> >>
    >> >> >> >> "Trefor" <> wrote in message news:...
    >> >> >> >> > 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
    >> >> >> >> >
    >> >> >> >> > --
    >> >> >> >> > Trefor
    >> >> >> >>
    >> >> >>
    >> >>

    >>
     
    Sue Mosher [MVP-Outlook], May 30, 2008
    #10
  11. Trefor

    Trefor Guest

    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.

    --
    Trefor


    "Sue Mosher [MVP-Outlook]" wrote:

    > > 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
    >
    >
    > "Trefor" <> wrote in message news:...
    > > 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
    > >
    > >
    > > --
    > > Trefor
    > >
    > >
    > > "Sue Mosher [MVP-Outlook]" wrote:
    > >
    > >> 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.
    > >>
    > >> "Trefor" <> wrote in message news:...
    > >> > 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?
    > >> >
    > >> > --
    > >> > Trefor
    > >> >
    > >> >
    > >> > "Sue Mosher [MVP-Outlook]" wrote:
    > >> >
    > >> >> > "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
    > >>
    > >> >>
    > >> >>
    > >> >> "Trefor" <> wrote in message news:...
    > >> >> > 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
    > >> >> > .
    > >> >> > .
    > >> >> >
    > >> >> >
    > >> >> > --
    > >> >> > Trefor
    > >> >> >
    > >> >> >
    > >> >> > "Sue Mosher [MVP-Outlook]" wrote:
    > >> >> >
    > >> >> >> 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" <> wrote in message news:D...
    > >> >> >> > 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
    > >> >> >> >
    > >> >> >>
    > >> >> >> >
    > >> >> >> > "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.
    > >> >> >> >>
    > >> >> >> >>
    > >> >> >> >> "Trefor" <> wrote in message news:...
    > >> >> >> >> > 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
    > >> >> >> >> >
    > >> >> >> >> > --
    > >> >> >> >> > Trefor
    > >> >> >> >>
    > >> >> >>
    > >> >>
    > >>

    >
     
    Trefor, May 30, 2008
    #11
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Thomas

    Changing between using Word as the editor and not using Word

    Mike Thomas, Nov 25, 2004, in forum: Microsoft Outlook VBA Programming
    Replies:
    1
    Views:
    173
    Ken Slovak - [MVP - Outlook]
    Nov 26, 2004
  2. Mark Rae

    NewMailEx vs. ItemAdd

    Mark Rae, May 6, 2006, in forum: Microsoft Outlook VBA Programming
    Replies:
    14
    Views:
    2,605
    Ken Slovak - [MVP - Outlook]
    Jun 15, 2006
  3. Ken Slovak - [MVP - Outlook]

    Re: Sending mail from NewMailEx deletes the mail from the Inbox

    Ken Slovak - [MVP - Outlook], Jan 10, 2007, in forum: Microsoft Outlook VBA Programming
    Replies:
    0
    Views:
    297
    Ken Slovak - [MVP - Outlook]
    Jan 10, 2007
  4. ben

    NewMailEx Only provides First e-mail in inbox

    ben, Oct 2, 2009, in forum: Microsoft Outlook VBA Programming
    Replies:
    2
    Views:
    740
  5. Ken Slovak - [MVP - Outlook]

    Re: NewMailEx event not Triggered

    Ken Slovak - [MVP - Outlook], Mar 24, 2010, in forum: Microsoft Outlook VBA Programming
    Replies:
    0
    Views:
    684
    Ken Slovak - [MVP - Outlook]
    Mar 24, 2010
Loading...

Share This Page