Not set to an instance of object RRS feed

  • Question

  • Hi everyone,

    I have a strange problem which I can't seem to find a solution for.  I am using VS2010 and have created an outlook 2007 add-in.  I have created a ribbon using the Ribbon Designer which appears whenever new e-mails are composed.  The ribbon contains 4 toggle buttons.  I have code in the Application_ItemSend event of ThisAddIn which checks the state of the 4 toggle buttons.  Most of the time this works, however occasionally this stops working and I receive the error object reference not set to an instance of the object.  Once I receive this error, the only thing that cures the problem is re-starting outlook, at which point everything works as expected...until the next time the error occurs.  Here is the code I am using:

    Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
            Dim olMi As Outlook.MailItem = Item
                Dim ribbonCollection As ThisRibbonCollection = Globals.Ribbons(Globals.ThisAddIn.Application.ActiveInspector())
                If ribbonCollection.Classification.btn1.Checked = True Then MsgBox("1")
                If ribbonCollection.Classification.btn2.Checked = True Then MsgBox("2")
                If ribbonCollection.Classification.btn3.Checked = True Then Msgbox("3")
                If ribbonCollection.Classification.btn4.Checked = True Then MsgBox("4")
            Catch ex As Exception
                MsgBox(ex.Message & vbCr & vbCr & "Please contact your system administrator.", MsgBoxStyle.OkOnly, "Add-in Error")
            End Try
        End Sub

    It appears to be random when the above code stops working, the error occurs on the If statement so I know it's referring to the ribbon called Classification.  Any ideas?

    Any help much appreciated.


    Thursday, February 23, 2012 11:43 AM


  • I bet it's the call to ActiveInspector that's throwing the exception; there's a great possibility that there's no open Outlook items during Application_ItemSend as that could fire after you click Send on the e-mail and the window closes.  If you want to trap the Send event for a specific e-mail after it is opened, trap the MailItem.Send event instead.

    Eric Legault
    MVP (Outlook)
    About me...

    Thursday, February 23, 2012 3:30 PM