0
votes

We created an Outlook VSTO add-in that stops working after leaving Outlook idle overnight. When we return to work the next day, the add-in still shows as enabled but is no longer working. You restart Outlook and it works fine again. We have about 35 users working out of the same inbox (Outlook 2013). All are set up the same way in Live Mode on Exchange Server with their personal email as the default sender and the shared "work" e-mail box proxied from their personal. Below is the VB code designed to just stamp every e-mail, a user deletes or moves to another folder, with the text "Email Handled By (username) At (now). We are open to alternative suggestions. Just looking for a way to track who deletes/handles an e-mail. We know we should move to a CRM type system (5,000+ emails a day). Thank you in advance for any help.

'Allow forms to be used, Future Possible enhancements
Imports System.Windows.Forms
'Allow use of the outlook interperability (Outlook functions 
Imports Microsoft.Office.Interop.Outlook
Imports System.Data
Imports System.Data.SqlClient
Public Class ThisAddIn
    'Create public variable holders
    'Variable to hold the folder items (Inbox)
    Public WithEvents olkFolder As Outlook.Folder
    'Variable to hold the folder Items (Deleted Item
    Public WithEvents olkFolderItems As Outlook.Folder
    'Variable to hold the item.
    Public WithEvents olkItems As Outlook.Items
    Public WithEvents olkFolderCT As Outlook.Folder
    Public WithEvents olkFolderItemsCT As Outlook.Folder
    Public WithEvents olkItemsCT As Outlook.Items
    Public WithEvents olkFolderNJ As Outlook.Folder
    Public WithEvents olkFolderItemsNJ As Outlook.Folder
    Public WithEvents olkItemsNJ As Outlook.Items
    Public WithEvents olkFolderOld As Outlook.Folder
    Public WithEvents olkFolderItemsOld As Outlook.Folder
    Public WithEvents olkItemsOld As Outlook.Items
    'Function that fires upon startup of the plugin
    Private Sub ThisAddIn_Startup(sender As Object, e As EventArgs) Handles Me.Startup
        'This is used to force the program to continue in case an operator does not have an inbox with a name of ROCLI
        On Error Resume Next
        'Set the variable to the Inbox Folder
        olkFolder = Application.Session.Folders("ROCLI").Folders("Inbox")
        'Set the variable to the Deleted Folder
        olkFolderItems = Application.Session.Folders("ROCLI").Folders("Deleted Items")
        'Set the items, to the items in the Inbox
        olkItems = Application.Session.Folders("ROCLI").Folders("Inbox").Items
        'Set the variable to the Inbox Folder
        olkFolderCT = Application.Session.Folders("ROCCTHV").Folders("Inbox")
        'Set the variable to the Deleted Folder
        olkFolderItemsCT = Application.Session.Folders("ROCCTHV").Folders("Deleted Items")
        'Set the items, to the items in the Inbox
        olkItemsCT = Application.Session.Folders("ROCCTHV").Folders("Inbox").Items
        'Set the variable to the Inbox Folder
        olkFolderNJ = Application.Session.Folders("ROCNJ").Folders("Inbox")
        'Set the variable to the Deleted Folder
        olkFolderItemsNJ = Application.Session.Folders("ROCNJ").Folders("Deleted Items")
        'Set the items, to the items in the Inbox
        olkItemsNJ = Application.Session.Folders("ROCNJ").Folders("Inbox").Items
        'Set the variable to the Inbox Folder
        olkFolderOld = Application.Session.Folders("[email protected]").Folders("Inbox")
        'Set the variable to the Deleted Folder
        olkFolderItemsOld = Application.Session.Folders("[email protected]").Folders("Deleted Items")
        'Set the items, to the items in the Inbox
        olkItemsOld = Application.Session.Folders("[email protected]").Folders("Inbox").Items
    End Sub
    'Function that is the handler which is fired before any item is moved
    Private Sub olkFolder_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, ByRef Cancel As Boolean) Handles olkFolder.BeforeItemMove, olkFolderCT.BeforeItemMove, olkFolderNJ.BeforeItemMove, olkFolderOld.BeforeItemMove
        'If the item is moved to any folder Add the HTML to the email
        If MoveTo.Name Is Nothing Then
            'Update the body of the item (Email)
            Item.HTMLBody = "<HTML><BODY><font size=4 color=red><b>Email Handled By " & System.Environment.UserName & " At " & Now() & "</b></font><BR><BR>" & Item.HTMLBody & "</body></html>"
            'Save the email, or else nothing will be changed on the server
            Item.Save
            'If its moved to any folder with a name (Failsafe)
        Else
            'Same as above
            Item.HTMLBody = "<HTML><BODY><font size=4 color=red><b>Email Handled By " & System.Environment.UserName & " At " & Now() & "</b></font><BR><BR>" & Item.HTMLBody & "</body></html>"
            'Same as above 
            Item.Save
        End If
        Dim UserName, DeletedTime, ReceiveTime, Subject, Body, SentFrom, SentTo As String
        UserName = Environment.UserName
        DeletedTime = Now()
        ReceiveTime = Item.ReceivedTime
        Subject = Item.Subject
        Body = Item.HTMLBody
        SentFrom = Item.SenderName
        SentTo = Item.To
    End Sub
End Class
1

1 Answers

0
votes

What's most likely happening is that the add-in is crashing because you have no error handling in the BeforeItemMove event. The MoveTo folder could be null if the user is permanently deleting the email, and the code will fail and the add-in will crash when you try to evaluate MoveTo.Name. Just handle that and wrap everything in a Try/Catch block and handle exceptions accordingly.