Unable to cast COM object

J

Jim Darlington

The Setup
------------
Windows XP SP2 - 64 Bit.
Visual Studio 2005 SP1
MS Office 2003 (Word, Excel, Access InfoPath, PowerPoint, Publisher and
Project).
..NET 2.0
Microsoft Project 11.0 Object Library


The Requirements:
----------------------
Create a small WinForms program that opens a certain MS Project file and
exports all the tasks found inside it.

The Code:
------------

MSProject.ApplicationClass oApp = new
Microsoft.Office.Interop.MSProject.ApplicationClass();

oApp.FileOpen(strFileName, true, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

foreach (MSProject.Project oProj in oApp.Projects)
{
foreach (MSProject.Task oTask in oProj.Tasks)
{
// Export the data
}
}


The Problem:
----------------
I'm getting the following exception on the first iteration of the inner loop:

Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.Office.Interop.MSProject.Task'. This operation failed because the
QueryInterface call on the COM component for the interface with IID
'{000C0C3F-0000-0000-C000-000000000046}' failed due to the following error:
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

Any help would be greatly appreciated. thanks
 
S

Somerset Steve

This is my first use of this Microsoft Service I hope I am doing this in the
right place

I am suffering a very similar experience getting trying to access multiple
tasks in an outlook public folder:

In the past week problems have started to occur that stop the entire
application from being able to save tasks. (My software is written using
VB.NET in Visual Studio 2005 and I use Visual Studio Tools for the Microsoft
Office (VSTO)

I have narrowed the problem down to what appears (to me) to be a bug in the
interop layer of the VSTO. It seems to lose track of its objects around about
160 items (140 items when not using cached mode ???!!!)

I get the same results when using VSTO SE on a VISTA machine with Outlook
2007

The error is:



System.InvalidCastException: Unable to cast COM object of type
'System.__ComObject' to interface type
'Microsoft.Office.Interop.Outlook.TaskItem'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
'{00063035-0000-0000-C000-000000000046}' failed due to the following error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).



It is definitely a taskitem

Here is the code that is experiencing the problem, it works fine until the
number of items returned by the Restrict statement exceeds 140-160 records.

It is operating on a public folder

The line that fails is the cast: task = CType(it,
Outlook.TaskItem)

(The code is now not quite as clean as I have tried to add checks etc. to
get around the problem - none of which have worked even if I stop processing
before 140 records are read - once the restrict command has executed the
application has to be shut down)


Private Sub RefreshData()

Dim task As Outlook.TaskItem

Dim objList As New Generic.List(Of Object)

Dim liCount As Integer = 0

list.Clear()

blList.Clear()

Application.DoEvents()

Dim items As Outlook.Items =
gbCallActionsFolder.Folder.Items.Restrict("[" & My.Settings.TaskUpnContactId
& "]='" & GetContactUserPropertyValue(ldcCustomer.Contact,
My.Settings.ContactUpnId) _

& "'")



' and [CreationTime] <= '" & Format(dtpSelectUpTo.Value.AddDays(1),
"D") _

' & "' and [CreationTime] > '" &
Format(dtpSelectUpTo.Value.AddDays(-6), "D") & "'")

' Dim items As Outlook.Items =
gbCallActionsFolder.Folder.Items.Restrict("[" & My.Settings.TaskUpnContactId
& "]='" & GetContactUserPropertyValue(ldcCustomer.Contact,
My.Settings.ContactUpnId) _

' & "' and [CreationTime] <= '" &
Format(dtpSelectUpTo.Value.AddDays(1), "D") _

' & "' and [CreationTime] > '" &
Format(dtpSelectUpTo.Value.AddDays(-6), "D") & "'")

MessageBox.Show("Items selected " & Str(items.Count))

Try

For Each it As Object In items

objList.Add(it)

Next

Catch ex As Exception

LogError(ex, True, "Error reading messages into temp area
resetting folder please try again")

gbCallActionsFolder.ResetFolder()

End Try

Try

For Each it As Object In objList

task = CType(it, Outlook.TaskItem)

liCount = liCount + 1

Dim ca As New CustomerCallAction(task)

list.Add(ca.ReverseSearchDate & Str(list.Count), ca)

If liCount > 120 Then

MessageBox.Show("Number of display items exceeded, only
120 to be displayed")

Exit For

End If

Next

Catch ex As Exception

LogError(ex, True, "Error reading message no:" & Str(liCount) &
" resetting folder please try again")

gbCallActionsFolder.ResetFolder()

End Try

objList = Nothing

items = Nothing

For Each kv As KeyValuePair(Of String, CustomerCallAction) In list

blList.Add(kv.Value)

Next



End Sub
 
R

Rod Gill

Hi,

Try asking this question in an Outlook ng as this one is dedicated to
Microsoft Project VBA. I think there are also VSTO groups.

--

Rod Gill
Project MVP

Project VBA Book, for details visit:
http://www.projectvbabook.com

NEW!! Web based VBA training course delivered by me. For details visit:
http://projectservertraining.com/learning/index.aspx


----------------------------------------------------------------------------------------------------


Somerset Steve said:
This is my first use of this Microsoft Service I hope I am doing this in
the
right place

I am suffering a very similar experience getting trying to access multiple
tasks in an outlook public folder:

In the past week problems have started to occur that stop the entire
application from being able to save tasks. (My software is written using
VB.NET in Visual Studio 2005 and I use Visual Studio Tools for the
Microsoft
Office (VSTO)

I have narrowed the problem down to what appears (to me) to be a bug in
the
interop layer of the VSTO. It seems to lose track of its objects around
about
160 items (140 items when not using cached mode ???!!!)

I get the same results when using VSTO SE on a VISTA machine with Outlook
2007

The error is:



System.InvalidCastException: Unable to cast COM object of type
'System.__ComObject' to interface type
'Microsoft.Office.Interop.Outlook.TaskItem'. This operation failed because
the QueryInterface call on the COM component for the interface with IID
'{00063035-0000-0000-C000-000000000046}' failed due to the following
error:
No such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE)).



It is definitely a taskitem

Here is the code that is experiencing the problem, it works fine until the
number of items returned by the Restrict statement exceeds 140-160
records.

It is operating on a public folder

The line that fails is the cast: task = CType(it,
Outlook.TaskItem)

(The code is now not quite as clean as I have tried to add checks etc. to
get around the problem - none of which have worked even if I stop
processing
before 140 records are read - once the restrict command has executed the
application has to be shut down)


Private Sub RefreshData()

Dim task As Outlook.TaskItem

Dim objList As New Generic.List(Of Object)

Dim liCount As Integer = 0

list.Clear()

blList.Clear()

Application.DoEvents()

Dim items As Outlook.Items =
gbCallActionsFolder.Folder.Items.Restrict("[" &
My.Settings.TaskUpnContactId
& "]='" & GetContactUserPropertyValue(ldcCustomer.Contact,
My.Settings.ContactUpnId) _

& "'")



' and [CreationTime] <= '" & Format(dtpSelectUpTo.Value.AddDays(1),
"D") _

' & "' and [CreationTime] > '" &
Format(dtpSelectUpTo.Value.AddDays(-6), "D") & "'")

' Dim items As Outlook.Items =
gbCallActionsFolder.Folder.Items.Restrict("[" &
My.Settings.TaskUpnContactId
& "]='" & GetContactUserPropertyValue(ldcCustomer.Contact,
My.Settings.ContactUpnId) _

' & "' and [CreationTime] <= '" &
Format(dtpSelectUpTo.Value.AddDays(1), "D") _

' & "' and [CreationTime] > '" &
Format(dtpSelectUpTo.Value.AddDays(-6), "D") & "'")

MessageBox.Show("Items selected " & Str(items.Count))

Try

For Each it As Object In items

objList.Add(it)

Next

Catch ex As Exception

LogError(ex, True, "Error reading messages into temp area
resetting folder please try again")

gbCallActionsFolder.ResetFolder()

End Try

Try

For Each it As Object In objList

task = CType(it, Outlook.TaskItem)

liCount = liCount + 1

Dim ca As New CustomerCallAction(task)

list.Add(ca.ReverseSearchDate & Str(list.Count), ca)

If liCount > 120 Then

MessageBox.Show("Number of display items exceeded, only
120 to be displayed")

Exit For

End If

Next

Catch ex As Exception

LogError(ex, True, "Error reading message no:" & Str(liCount) &
" resetting folder please try again")

gbCallActionsFolder.ResetFolder()

End Try

objList = Nothing

items = Nothing

For Each kv As KeyValuePair(Of String, CustomerCallAction) In list

blList.Add(kv.Value)

Next



End Sub



Jim Darlington said:
The Setup
------------
Windows XP SP2 - 64 Bit.
Visual Studio 2005 SP1
MS Office 2003 (Word, Excel, Access InfoPath, PowerPoint, Publisher and
Project).
.NET 2.0
Microsoft Project 11.0 Object Library


The Requirements:
----------------------
Create a small WinForms program that opens a certain MS Project file and
exports all the tasks found inside it.

The Code:
------------

MSProject.ApplicationClass oApp = new
Microsoft.Office.Interop.MSProject.ApplicationClass();

oApp.FileOpen(strFileName, true, Type.Missing, Type.Missing,
Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
MSProject.PjPoolOpen.pjPoolReadOnly, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

foreach (MSProject.Project oProj in oApp.Projects)
{
foreach (MSProject.Task oTask in oProj.Tasks)
{
// Export the data
}
}


The Problem:
----------------
I'm getting the following exception on the first iteration of the inner
loop:

Unable to cast COM object of type 'System.__ComObject' to interface type
'Microsoft.Office.Interop.MSProject.Task'. This operation failed because
the
QueryInterface call on the COM component for the interface with IID
'{000C0C3F-0000-0000-C000-000000000046}' failed due to the following
error:
Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

Any help would be greatly appreciated. thanks
 

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