Good Morning Andrei,
As always thanks for your insightful and helpful reply!
I've now managed to fix the problem when opening an MSG file using your advice above. However, this has now resulted in another issue. My updated code in the ProcessOpen method is below:
Public Overrides Sub ProcessOpen(ByVal E As AddinExpress.MSO.ADXCancelEventArgs)
TempestaEmail(E)
End Sub
Private Sub TempestaEmail(ByVal E As AddinExpress.MSO.ADXCancelEventArgs)
Dim OutlookApp As Outlook._Application = Nothing
Dim MSG As Outlook.MailItem = Nothing
Dim mailRecipients As Outlook.Recipients = Nothing
Dim msgRecipients As Outlook.Recipients = Nothing
Dim mailItem As Outlook._MailItem = Nothing
Try
If TypeOf Me.ItemObj Is Outlook._MailItem Then
mailItem = Me.ItemObj
' meta tag is set in the email body inside tempesta generated eml files.
' this helps us to identify these emails efficiently.
' html meta tags do not get carried across to the msg file.
If InStr(mailItem.HTMLBody, "<meta name=""tempesta-email"" content=""true"">") = 0 Then
Exit Sub
End If
' cancel the original process.
E.Cancel = True
OutlookApp = DirectCast(Me.Module, AddinModule).OutlookApp
'' create a new msg mailitem.
MSG = CType(OutlookApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
MSG.Subject = mailItem.Subject
Dim mailItemBody As String = mailItem.HTMLBody
mailRecipients = mailItem.Recipients
msgRecipients = MSG.Recipients
If mailRecipients.Count > 0 Then
For i As Integer = 1 To mailRecipients.Count
Dim recipient As Outlook.Recipient = mailRecipients.Item(i)
Dim added = msgRecipients.Add(recipient.Address).Type = recipient.Type
If recipient IsNot Nothing Then
Marshal.ReleaseComObject(recipient)
recipient = Nothing
End If
Next
End If
msgRecipients.ResolveAll()
''' to maintian signature display() must be called before the contents is applied.
MSG.Display()
MSG.HTMLBody = mailItemBody & MSG.HTMLBody
End If
Finally
If mailRecipients IsNot Nothing Then
Marshal.ReleaseComObject(mailRecipients)
mailRecipients = Nothing
End If
If msgRecipients IsNot Nothing Then
Marshal.ReleaseComObject(msgRecipients)
msgRecipients = Nothing
End If
If OutlookApp IsNot Nothing Then
Marshal.ReleaseComObject(OutlookApp)
OutlookApp = Nothing
End If
End Try
End Sub
As you can see, there's a line that checks for a meta header in the body of the opened email (in this case it's an EML file, we have produced in a separate system). When opening this EML file, the plugin detects this header and should take the content of the EML and produce a new email with that content. The reason for doing this is to add the user's email signature to the bottom of the email. When opening the EML file I now receive several exception pop ups, both of these exceptions are of type InvalidComObjectException and the associated message is: COM object that has been separated from its underlying RCW cannot be used. Stack traces are as follows:
Detailed technical information follows:
---
Date and Time: 23/May/2024 10:50:32
Machine Name: 3BX6BK3
IP Address: 192.168.1.185
Current User: HGFcarter
Application Domain: C:projectseFiles-Office-IntegrationeFileseFilesinDebug
Assembly Codebase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
Assembly Full Name: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Assembly Version: 4.0.0.0
Assembly Build Date: 06/Apr/2024 03:51:18
Exception Source: mscorlib
Exception Type: System.Runtime.InteropServices.InvalidComObjectException
Exception Message: COM object that has been separated from its underlying RCW cannot be used.
Exception Target Site: InvokeDispMethod
---- Stack Trace ----
System.RuntimeType.InvokeDispMethod(name As String, invokeAttr As BindingFlags, target As Object, args As Object[], byrefModifiers As Boolean[], culture As Int32, namedParameters As String[])
AddinExpress.OL.2005.dll: N 00000 (0x0) JIT
System.RuntimeType.InvokeMember(name As String, bindingFlags As BindingFlags, binder As Binder, target As Object, providedArgs As Object[], modifiers As ParameterModifier[], culture As CultureInfo, namedParams As String[])
AddinExpress.OL.2005.dll: N 0492 (0x1EC) IL
System.Type.InvokeMember(name As String, invokeAttr As BindingFlags, binder As Binder, target As Object, args As Object[])
AddinExpress.OL.2005.dll: N 0000 (0x0) IL
AddinExpress.OL.OutlookUtils.ExplorersCount(OutlookAppObj As Object)
AddinExpress.OL.2005.dll: N 0040 (0x28) IL
Detailed technical information follows:
---
Date and Time: 23/May/2024 10:50:49
Machine Name: 3BX6BK3
IP Address: 192.168.1.185
Current User: HGFcarter
Application Domain: C:projectseFiles-Office-IntegrationeFileseFilesinDebug
Assembly Codebase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
Assembly Full Name: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Assembly Version: 4.0.0.0
Assembly Build Date: 06/Apr/2024 03:51:18
Exception Source: mscorlib
Exception Type: System.Runtime.InteropServices.InvalidComObjectException
Exception Message: COM object that has been separated from its underlying RCW cannot be used.
Exception Target Site: InvokeDispMethod
---- Stack Trace ----
System.RuntimeType.InvokeDispMethod(name As String, invokeAttr As BindingFlags, target As Object, args As Object[], byrefModifiers As Boolean[], culture As Int32, namedParameters As String[])
AddinExpress.OL.2005.dll: N 00000 (0x0) JIT
System.RuntimeType.InvokeMember(name As String, bindingFlags As BindingFlags, binder As Binder, target As Object, providedArgs As Object[], modifiers As ParameterModifier[], culture As CultureInfo, namedParams As String[])
AddinExpress.OL.2005.dll: N 0492 (0x1EC) IL
System.Type.InvokeMember(name As String, invokeAttr As BindingFlags, binder As Binder, target As Object, args As Object[])
AddinExpress.OL.2005.dll: N 0000 (0x0) IL
AddinExpress.OL.OutlookUtils.InspectorsCount(OutlookAppObj As Object)
AddinExpress.OL.2005.dll: N 0040 (0x28) IL
The only difference is ExplorersCount vs InspectorsCount in both exceptions. I also recieve the same exceptions when closing the mail message.
I've previously encountered this problem when releasing COM objects too early which I've successfully resolved however am struggling in this instance.
I've also tried to create a new project with just the basics included, AddinModule, OutlookItemEvents and the issue is not repeatable either.
Do you have any advice on how to resolve this issue? |