0
votes

I have (using information from SO) implemented a VBA macro that runs to process new emails after the 'run a script' options was removed from the Outlook rules. I do this as follows:

Private WithEvents Items As Outlook.Items

Public Sub Application_Startup()
' Add an inbox event listener
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  MsgBox "Startup macro run"

End Sub

Private Sub Items_ItemAdd(ByVal item As Object)
  'Do something on new email arrival
.
.
.
End Sub

However the inbox listener appears to frequently stop working and I either have to restart Outlook or re-run the 'Startup' macro manually to kick it back into life - this appears to be a common problem with no solution.

Question - I'm not a VBA expert and I was wondering if I can I simply re-run the 'Application_Startup' macro to restart the listener at the end of the 'Items_ItemAdd(ByVal item As Object)' macro?

3
That won't do you any good - you know the event listener works when Items.ItemAdd event fires. And if it doesn't, ItemAdd won't fire anyway. - Dmitry Streblechenko
@DmitryStreblechenko well I don’t know that as I’m new to Outlook VBA 😀 but I know that it stops working and I know that restarting Outlook appears to get it working again, so I’m wondering how to keep it working when all my searching has revealed this to be a known issue with no solution. Hopefully the solution posted as an answer will do the trick. - Steve Ives
Run a code every half an hour stackoverflow.com/questions/12257985/… - niton

3 Answers

1
votes

If you need to reset the ItemAdd event handler, I don't think using ItemAdd to do that makes any sense.

You can either use a timer (which does not exist in VBA) or you can use some other event that fires more or less frequently, such as the Explorer.SelectionChange event (Explorer can be retrieved from Application.ActiveExplorer).

0
votes

You could add a new macro that does the actions originally present in the Application_Startup event. Then you could later refer to that sub at the end of your Items_ItemAdd macro.

Private WithEvents Items As Outlook.Items

Public Sub Application_Startup()
   Call startupevents
End Sub

Sub startupevents()

' Add an inbox event listener
  Dim olApp As Outlook.Application
  Dim objNS As Outlook.NameSpace
  Set olApp = Outlook.Application
  Set objNS = olApp.GetNamespace("MAPI")
  ' default local Inbox
  Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
  MsgBox "Startup macro run"

End Sub

Private Sub Items_ItemAdd(ByVal item As Object)
  'Do something on new email arrival
'
'
'
Call startupevents
End Sub
0
votes

Simpler code may have an impact.

Private WithEvents Items As Outlook.Items

Public Sub Application_Startup()
    ' Add an inbox event listener
    Dim objNS As Outlook.NameSpace

    ' The code is in Outlook, not being called, for example, from Excel.
    Set objNS = Session.GetNamespace("MAPI")
    ' default local Inbox
    Set Items = objNS.GetDefaultFolder(olFolderInbox).Items
    MsgBox "Startup macro run"
End Sub

If the above has no impact then this may rerun Application_Startup frequently enough.

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Application_Startup
End Sub