Add-in fires exception. Cure is to display MsgBox

Add-in Express™ Support Service
That's what is more important than anything else

Add-in fires exception. Cure is to display MsgBox
Add-in fires exception. Exception Target Site: get_Application 
Gordon Prince


Guest


I've added code to OutlookItemsEventsClass's ItemAdd(). I had some MsgBox entries while building the code, everything seemed to work ok. Now when I remove the debugging MsgBox entries, I get an exception error when the code leaves the ItemAdd() procedure.

Detailed technical information follows: 
---
(Inner Exception)
Date and Time:         11/5/2015 5:54:27 AM
Machine Name:          TEKHELPS7X64
IP Address:            fe80::b1ad:86c:b9fa:4227%27
Current User:          TEKHELPS7X64Gordon

Application Domain:    C:UsersGordonSourceReposGKBMOutlookGKBMOutlookGKBMOutlookinDebug
Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Outlook/14.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll
Assembly Full Name:    Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c
Assembly Version:      14.0.0.0

Exception Source:      Microsoft Outlook
Exception Type:        System.Runtime.InteropServices.COMException
Exception Message:     The operation failed.
Exception Target Site: get_Application

---- Stack Trace ----
   Microsoft.Office.Interop.Outlook._MailItem.get_Application()
       Microsoft.Office.Interop.Outlook.dll: N 00000 (0x0) JIT 
   GKBMOutlook.OutlookItemsEventsClass1.ItemAdd(Item As Object, SourceFolder As Object)
       OutlookItemsEventsClass1.vb: line 0043, col 13, IL 0043 (0x2B)
   AddinExpress.MSO.ADXOlItemsEvents_SinkHelper.AddinExpress.MSO.IFolderItemsEvents.ItemAdd(item As Object)
       Microsoft.Office.Interop.Outlook.dll: N 0012 (0xC) IL 



(Outer Exception)
Date and Time:         11/5/2015 5:54:27 AM
Machine Name:          TEKHELPS7X64
IP Address:            fe80::b1ad:86c:b9fa:4227%27
Current User:          TEKHELPS7X64Gordon

Application Domain:    C:UsersGordonSourceReposGKBMOutlookGKBMOutlookGKBMOutlookinDebug
Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/AddinExpress.MSO.2005/7.7.4087.2005__4416dd98f0861965/AddinExpress.MSO.2005.dll
Assembly Full Name:    AddinExpress.MSO.2005, Version=7.7.4087.2005, Culture=neutral, PublicKeyToken=4416dd98f0861965
Assembly Version:      7.7.4087.2005

Exception Source:      
Exception Type:        AddinExpress.MSO.ADXExternalException
Exception Message:     An error has occurred in the code of the add-in.
Exception Target Site: Object reference not set to an instance of an object.

---- Stack Trace ----

As below:

                ' without the MsgBox here I get an error
                MsgBox("The E-mail's EntryID was updated in InstantFile.", vbOKOnly, "Outlook Add-in")
                Exit Sub


Seems like opening the MsgBox might be triggering creating a reference to the pop-up window, closing it doesn't cause any problems. Maybe I need something in place of the MsgBox to release a reference or point to something new, etc.

Any suggestions much appreciated.
Posted 05 Nov, 2015 04:57:29 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
Hello Gordon,

In the other topic I recommended that you don't use message boxes: a message box generates an extra pair of Deactivate and Activate events which may influence the behavior of your add-in.

Instead, you can add debug messages using System.Diagnostics.Debug.WriteLine(). To collect such messages at run time, you can use the DebugView utility, see http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx.

The stack trace points to this line of this file:
OutlookItemsEventsClass1.vb: line 0043, col 13, IL 0043 (0x2B)

It looks like you call MailItem.Application in your code. You always need to use the OutlookApp property of the add-in module instead. That property provides the Outlook.Application instance which is regarded as "safe" by Outlook; all other instances (including MailItem.Application) are "unsafe"; using them may cause security warnings. To get the OutlookApp property use this code line: {add-in project name e.g MyAddin1}.AddinModule.CurrentInstance.OutlookApp.


Andrei Smolin
Add-in Express Team Leader
Posted 05 Nov, 2015 06:54:49 Top
Gordon Prince


Guest


Last week when you suggested add debug messages using System.Diagnostics.Debug.WriteLine() it didn't work for me. But I was having some other problems and the whole .Debug wasn't working. But the project is behaving better now and .Debug works.

But I'm not getting the result that I think you're telling me I should. So I wonder if I'm misunderstanding your suggestion.

                ' MsgBox("The E-mail's EntryID was updated in InstantFile.", vbInformation + vbOKOnly, "GKBM Outlook Add-in")
                Debug.WriteLine("The E-mail's EntryID was updated in InstantFile.")

If I leave in the MsgBox, everything works, including I see the Debug.WriteLine results in the debug window. But if I comment out the MsgBox, then I see the text in the debug window, then I get the error. So am I implementing this as you meant? Or what is going on?

Thanks for your comment about
"You always need to use the OutlookApp property of the add-in module instead"
so I will review the code and update it as you've recommended.

Thanks.
Posted 05 Nov, 2015 07:27:59 Top
Andrei Smolin


Add-in Express team


Posts: 18806
Joined: 2006-05-11
Gordon Prince writes:
then I get the error.


I suggest to fix the exception by replacing MailItem.Appliation with calling OutlookApp.


Andrei Smolin
Add-in Express Team Leader
Posted 05 Nov, 2015 09:56:12 Top
Gordon Prince


Guest


Sounds good. My inexperience with VS & VB.NET is showing again. I don't know how to do that.

I found this an another one of your posts.
"In the add-in module, there is a property called OutlookApp. If you run that code outside of the module, then you can access the module via casting the return value of AddinExpress.MSO.ADXAddinModule.CurrentInstance to the module type, say to MyAddin1.AddinModule."

I think that's what I'm trying to do. So how would I do that? This goes to me understanding procedure and variable scope, but not being sure how to implement it in VB.NET.

From another of your support topics, I found some VB.NET code that leads me to

    Dim outlookApp As AddinModule = CType(AddinModule.CurrentInstance.OutlookApp(), AddinModule)

in my OutlookItemsEventsClass12.vb module, but that doesn't compile.

Can you give me a Dim statement and tell me where put it?

Thanks.
Posted 05 Nov, 2015 10:51:18 Top
Gordon Prince


Guest


This seems to work.
    
Dim OutlookApp As Outlook.Application = CType(AddinModule.CurrentInstance, AddinModule).OutlookApp

    Public Sub New(ByVal ADXModule As AddinExpress.MSO.ADXAddinModule)
        MyBase.New(ADXModule)
    End Sub

    Public Overrides Sub ItemAdd(ByVal Item As Object, ByVal SourceFolder As Object)
        If TypeOf Item Is Outlookapp.MailItem Then

Is this what you meant?
Posted 05 Nov, 2015 11:05:12 Top
Gordon Prince


Guest


I made the change so my procedure now refers only to OutlookApp. The error still occurs, but it's in a different place than it was, so you're on the right track. Here's the new error message:
Detailed technical information follows: 
---
(Inner Exception)
Date and Time:         11/5/2015 1:04:12 PM
Machine Name:          TEKHELPS7X64
IP Address:            fe80::b1ad:86c:b9fa:4227%27
Current User:          TEKHELPS7X64Gordon

Application Domain:    C:UsersGordonSourceReposGKBMOutlookGKBMOutlookGKBMOutlookinDebug
Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Outlook/14.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Outlook.dll
Assembly Full Name:    Microsoft.Office.Interop.Outlook, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c
Assembly Version:      14.0.0.0

Exception Source:      Microsoft Outlook
Exception Type:        System.Runtime.InteropServices.COMException
Exception Message:     The operation failed.
Exception Target Site: get_EntryID

---- Stack Trace ----
   Microsoft.Office.Interop.Outlook._MailItem.get_EntryID()
       Microsoft.Office.Interop.Outlook.dll: N 00000 (0x0) JIT 
   GKBMOutlook.OutlookItemsEventsClass1.ItemAdd(Item As Object, SourceFolder As Object)
       OutlookItemsEventsClass1.vb: line 0059, col 09, IL 0228 (0xE4)
   AddinExpress.MSO.ADXOlItemsEvents_SinkHelper.AddinExpress.MSO.IFolderItemsEvents.ItemAdd(item As Object)
       Microsoft.Office.Interop.Outlook.dll: N 0012 (0xC) IL 



(Outer Exception)
Date and Time:         11/5/2015 1:04:12 PM
Machine Name:          TEKHELPS7X64
IP Address:            fe80::b1ad:86c:b9fa:4227%27
Current User:          TEKHELPS7X64Gordon

Application Domain:    C:UsersGordonSourceReposGKBMOutlookGKBMOutlookGKBMOutlookinDebug
Assembly Codebase:     file:///C:/Windows/assembly/GAC_MSIL/AddinExpress.MSO.2005/7.7.4087.2005__4416dd98f0861965/AddinExpress.MSO.2005.dll
Assembly Full Name:    AddinExpress.MSO.2005, Version=7.7.4087.2005, Culture=neutral, PublicKeyToken=4416dd98f0861965
Assembly Version:      7.7.4087.2005

Exception Source:      
Exception Type:        AddinExpress.MSO.ADXExternalException
Exception Message:     An error has occurred in the code of the add-in.
Exception Target Site: Object reference not set to an instance of an object.

---- Stack Trace ----

Right away I see this line of the error is different:
Exception Target Site: get_EntryID
It was "get_Application" in the original error.

I'll poke around trying to figure this out, but if it makes sense to you about where to look and what I'm looking for, it would be a great help to me.

Cheers.
Posted 05 Nov, 2015 12:13:33 Top
Gordon Prince


Guest


Also when I [Start] debugging from VS I get 4 of these messages in my Immediate Window:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in GKBMOutlook.dll
in case those point to the source of the trouble.

Thanks.
Posted 05 Nov, 2015 12:16:32 Top
Gordon Prince


Guest


I changed the Exceptions options in VS and now the code stops where the problem begins. It appears that without the MsgBox command the procedure
Public Overrides Sub ItemAdd(ByVal Item As Object, ByVal SourceFolder As Object)

fires a second time. Somehow some of the properties of "Item" that were available the first time through are not available the second time through. The second time through ItemAdd the error is thrown. I can change the code and move the error around. The code starts with

        If TypeOf Item Is Outlook.MailItem Then
            myMailItem = Item

In the debugger I print myMailItem.Subject to make sure I've working with the same item each time (I am).
First time through I test myMailItem.BillingInformation and find or don't find what I'm testing for.
Second time through the test fails on
If Len(myMailItem.BillingInformation) > 0 Then

with
System.Runtime.InteropServices.COMException was unhandled by user code
  ErrorCode=-1456209657
  HResult=-1456209657
  Message=The operation failed.
  Source=Microsoft Outlook
  StackTrace:
       at Microsoft.Office.Interop.Outlook._MailItem.get_BillingInformation()
       at GKBMOutlook.OutlookItemsEventsClass1.ItemAdd(Object Item, Object SourceFolder)
       at AddinExpress.MSO.ADXOlItemsEvents_SinkHelper.AddinExpress.MSO.IFolderItemsEvents.ItemAdd(Object item)
  InnerException: 


When MsgBox is the statement ahead of the Exit Sub statement, the execution path doesn't enter ItemAdd a second time. Instead it goes off through
Public Overrides Function GetContainer() As System.ComponentModel.IContainer

So why would Exit Sub trigger reentering the procedure? Why would the Item be different the second time around?

Can I call the same logic that's making this work after the MsgBox right before the Exit Sub statement and thereby solve my problem?

Over and out for this evening.

Cheers.
Posted 05 Nov, 2015 18:28:09 Top
Gordon Prince


Guest


I stuck this code in at the top of the procedure
        
Public Overrides Sub ItemAdd(ByVal Item As Object, ByVal SourceFolder As Object)
    Dim myMailItem As Outlook.MailItem
        If TypeOf Item Is Outlook.MailItem Then
            myMailItem = Item
        Else
            Exit Sub
        End If
        Try
            If myMailItem.EntryID = strLastID Then
                Exit Sub
            Else
                strLastID = myMailItem.EntryID
            End If
        Catch ex As Exception
            Exit Sub
        End Try
        ...
        Debug.Print "Finished executing my code."
        Exit sub
End Sub

Stepping through with the debugger, the first time everything runs. I've commented out the MsgBox that I've had in there the last two days and left only the Debug.Print statement, which fires right before the Exit Sub, and I can see that the Exit Sub statement was reached. Then the debugger steps back into the procedure, the Try/Catch throws an error and executes the Exit Sub statement there. So the procedure runs once, and I've accomplished what I wanted.

Maybe this is behavior that's part of Outlook, not your Add-in tools. Any thoughts on what's going on?

Cheers.
Posted 06 Nov, 2015 06:47:02 Top