0

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?

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • 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 Apr 10 '19 at 15:57
  • @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 Apr 10 '19 at 20:00
  • Run a code every half an hour https://stackoverflow.com/questions/12257985/outlook-vba-run-a-code-every-half-an-hour – niton Apr 12 '19 at 00:56

3 Answers3

1

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

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • I suppose the underlying question of "Why does the ItemAdd event handler stop working" remains unanswered? I've searched but found no answer. just suggestions such as set all variables to 'nothing' and 'add an empty `Application_Quit()`' – Steve Ives Apr 12 '19 at 09:07
  • It can stop firing if a connection with the serve is lost, but that only applies to the online (as opposed to cached) mode. – Dmitry Streblechenko Apr 12 '19 at 13:35
  • Thanks for the input. I made my routine to set the category based upon words in the subject a public sub, so if it stops working, I can run it manually and also re-start the inbox listener manually. – Steve Ives Apr 16 '19 at 09:51
  • I have noticed the occasional error from addItem - "13 - type mismatch", so I have expanded the error handler routine to also print the subject. Maybe this error is the cause of addItems stopping working or maybe it is a symptom of it being broken, – Steve Ives Apr 16 '19 at 09:53
  • You really need to show the relevant snippets of yurt code. Keep in mind that Inbox can contain items other than MailItem, such as MeetingItem, ReportItem, etc. – Dmitry Streblechenko Apr 16 '19 at 15:43
0

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
Tim Stack
  • 3,209
  • 3
  • 18
  • 39
  • I don't see how this can work - if ItemAdd event fires, you know that everything is good and there is no need to reset the event handler. And if ItemAdd event does not fire, your code resetting the event handler won't run anyway. – Dmitry Streblechenko Apr 10 '19 at 20:03
  • @DmitryStreblechenko it depends on when & why the event handler is stopping. `ItemAdd` seems to work one or twice before stopping working. If it gets disabled after running a few times once initiailised, then reinitialising it from `startupevents` every time should keep it working. – Steve Ives Apr 12 '19 at 09:05
  • @Tim - just confirmed that this isn't working. I can re-run the `startupEvents`manually and that get's it working again, so the Inbox listener is failing at some other point and `Items_ItemAdd`is not getting called at all. – Steve Ives Apr 12 '19 at 09:32
  • Thanks for the input. I made my routine to set the category based upon words in the subject a public sub, so if it stops working, I can run it manually and also re-start the inbox listener manually. – Steve Ives Apr 16 '19 at 09:51
0

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
niton
  • 8,771
  • 21
  • 32
  • 52