CType Not Working on System.__ComObject

D

Don

Using VS 2005 and VB.NET. In the sample code below, GetFirst returns an Item
Object, but it's wrapped in the System.__ComObject type. When I try to use
CType to get a MailItem object, I get the error "No Such Interface
Supported". And if I try to use lobjItem properties directly, such as
lobjItem.Subject, I get the error "Option Strict On disallows late binding".
Anyone have a solution for this? Thanks!


Dim lobjFolder As Outlook.MAPIFolder = Nothing
Dim lobjFolderItems As Outlook.Items = Nothing
Dim lobjFolders As Outlook.Folders = Nothing
Dim lobjMI As Outlook.MailItem = Nothing
Dim lobjItem As Object = Nothing
Dim lstrTypeName as String = ""
Dim lstrSubject as String = ""

'---more code before this
If lobjFolders.Count > 0 Then
lobjFolder = lobjFolders.Item(1)
lobjFolderItems = lobjFolder.Items
lobjItem = lobjFolderItems.GetFirst
lstrTypeName = TypeName(lobjItem).ToUpper
If lstrTypeName = "MAILITEM" Then
lobjMI = CType(lobjItem, Outlook.MailItem)
lstrSubject = lobjMI.Subject.Trim
'---more code...
End If
End If
 
W

Walter Wang [MSFT]

Hi Don,

I'm using following code in VS2005 and I'm not able to reproduce the issue
you mentioned:

Option Strict On

Imports System
Imports Microsoft.Office.Interop

Module Module1
Sub Main()
Dim lobjFolder As Outlook.MAPIFolder = New
Outlook.Application().GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaul
tFolders.olFolderInbox)
Dim lobjItem As Object = lobjFolder.Items.GetFirst()
Dim lstrTypeName As String = TypeName(lobjItem).ToUpper
If lstrTypeName = "MAILITEM" Then
Dim lobjMI As Outlook.MailItem = CType(lobjItem,
Outlook.MailItem)
Console.WriteLine(lobjMI.Subject.Trim())
End If
End Sub
End Module


I'm using Outlook 2007, which version of Outlook you're using?

Normally this error "Option Strict On disallows late binding" will occur at
compile time if I turned on Option Strict and using lObjItem.Subject
directly.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Don

Hi Walter,

My Bad. My example did not include enough code to replicate the problem. The
CType must be in a loop and each Mail Item must be deleted. Please try the
following example using VS2005 and either Outlook 2000 or Outlook 2003
(Note: though the mail items should reappear in your Deleted Items folder,
please still place only test messages in your inbox before running this
code).

CType works on the first Mail Item but fails on the second one with "No Such
Interface Supported". However, after looking at your code and running a few
more tests of my own, I believe the following fix will work: In the example
code below, change "lobjItem = lobjFolderItems.GetFirst()" to "lobjItem =
lobjFolder.Items.GetFirst()". CType will then work without error every time
through the loop. Any idea why the use of lobjFolderItems causes the error?


Dim lstrFailureReason As String = ""

Try
Dim lintCount As Int32
Dim lstrMsg As String
Dim oa As New Outlook.Application
Dim ons As Outlook.NameSpace = oa.GetNamespace("MAPI")
Dim lobjFolder As Outlook.MAPIFolder
Dim lobjItem As Object
Dim lobjFolderItems As Outlook.Items
Dim lobjMI As Outlook.MailItem
lobjFolder =
ons.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
lobjFolderItems = lobjFolder.Items
If lobjFolderItems.Count > 1 Then
For lintCount = 1 To 2
lobjItem = lobjFolderItems.GetFirst()
If TypeName(lobjItem).ToUpper = "MAILITEM" Then
lobjMI = CType(lobjItem, Outlook.MailItem)
lstrMsg = "Deleted Msg " & lintCount.ToString & ": "
& _
lobjMI.Subject.Trim() & ControlChars.CrLf
'---delete the mail item
lobjMI.Delete()
'---display progress
Me.TextBox1.Text = Me.TextBox1.Text & lstrMsg
Else
'---display error
Me.TextBox1.Text = "Error: One or more messages are
not Mail Items."
Exit For
End If
Next lintCount
End If


Catch objEX As Exception
If TypeOf objEX Is COMException Then
Dim objComEX As COMException = CType(objEX, COMException)
Dim objEX2 As Exception =
Marshal.GetExceptionForHR(objComEX.ErrorCode And Not &H7FF00000)
lstrFailureReason = "ComEX: " & objEX2.Message
objComEX = Nothing
objEX2 = Nothing
Else
lstrFailureReason = "EX: " & objEX.Message
End If
Me.TextBox1.Text = Me.TextBox1.Text & lstrFailureReason
End Try
 
W

Walter Wang [MSFT]

Hi Don,

It's strange. I managed to reproduce the issue once on a system with VS2005
SP1 + Outlook 2003. I saw exactly the exception you mentioned. This also
caused Outlook 2003 crashed and restarted; since later it failed to start
even in Safe mode, I chose to repair the Outlook 2003 installation. After
repairing, the issue seems gone. I'm not sure if this is the problem of the
installation.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

Don

Hi Walter,

More tests; same error result. I compiled and installed the test on a
machine that had Office 2003 but no VS2005 (just the Framework 1.1 and 2.0
redists). I tried Outlook's "Detect and Repair", and even Windows Update and
Office Update. Still got the error in CType attempting to process the second
Mail Item. I also tried adding "Marshal.ReleaseComObject(lobjItem)" and
"Marshal.ReleaseComObject(lobjMI)" to the loop, which are needed when
processing large numbers of items, but the error still occurred. So I guess
I'll use the solution I mentioned previously: use "lobjItem =
lobjFolder.Items.GetFirst()" instead of "lobjItem =
lobjFolderItems.GetFirst()".

Thanks,
Don
 
W

Walter Wang [MSFT]

Hi Don,

Thank you for the update. I'm still no luck to reproduce the issue on my
side. Also tried following VBA code in Outlook:

Sub test()
Dim inbox As MAPIFolder
Set inbox =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
Dim items As items
Set items = inbox.items
If items.Count > 1 Then
Dim i As Integer
For i = 1 To 2
Dim mi As MailItem
Set mi = items.GetFirst()
mi.Delete
Next
End If
End Sub

This might be related to how the Items property get cached or some unknown
cause. Anyway, let's stick with the workaround before we could find the
root cause. (Unfortunately I will have to find a reliable way to reproduce
the issue first...)

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Don,

I got some confirmed information on the root cause of this issue.

First, it's not related to com interop.

To minimize the perf penalty, the Items collection caches "windows" of rows
out of the underlying MAPI table, and it doesn't register for table
notifications. So when you ask for the first item after deleting the first
item, you're basically trying to re-open the deleted item, because the
previously cached window of rows is still presumed valid (no notifications
to invalidate it). If we use the MAPIFolder.Items again, we're basically
getting a new window of rows.

The issue is that opening a deleted item by this "error" will actually work
on some store providers and fail on others, depending on how a given store
provider implements entry IDs when messages are moved.

In summary, we should not use the cached Items reference when items from it
will be deleted.

We're sorry for the inconvenience caused by this issue. I hope above
explanation helps.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top