473,847 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Looping through Outlook Contacts gives me a 'Invalid Cast Exception'

I'm trying to loop through an exchange public folder contact list, get some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing
Nov 21 '05 #1
7 3633
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing

Nov 21 '05 #2
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing

Nov 21 '05 #3
Hi,

I get the same error with one of the items in my contacts. That is
the reason for the try catch block in my sample.

Ken
------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing


Nov 21 '05 #4
And the reason that it happens is that a folder is not required to contain homogeneous items. Contacts folders in particular usually also contain distribution lists. You may want to use Items.Restrict to get only items where the message class is IPM.Contact.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message news:Or******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I get the same error with one of the items in my contacts. That is
the reason for the try catch block in my sample.

Ken
------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount & "."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing



Nov 21 '05 #5
Sue,
I'm having trouble with the items.RESTRICT( "IPM.Contac t") code... how do you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlook code.com> wrote in message
news:O%******** ********@TK2MSF TNGP12.phx.gbl. ..
And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I get the same error with one of the items in my contacts. That is
the reason for the try catch block in my sample.

Ken
------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once
i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount &
"."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing


Nov 21 '05 #6
Did you read the Help topic on the Restrict method? You need to specify what field to look in as well as what value to look for:

strFind = "[MessageClass] = " & Chr(34) & "IPM.Contac t" & Chr(34)
Set colItems = = myFolder.Items. Restrict(strFin d)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message news:ej******** *****@TK2MSFTNG P15.phx.gbl...
Sue,
I'm having trouble with the items.RESTRICT( "IPM.Contac t") code... how do you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlook code.com> wrote in message
news:O%******** ********@TK2MSF TNGP12.phx.gbl. ..
And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I get the same error with one of the items in my contacts. That is
the reason for the try catch block in my sample.

Ken
------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable. I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once
i
get this error and try to go to the next record.. every record after gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount &
"."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing



Nov 21 '05 #7
Sue, I will try that. Thank you so much for your help! You, your books,
and your website have been a tremendous resource to me over the years.
Keep up the great work!
Thanks again!
Chris Thunell
ct******@pierce associates.com

"Sue Mosher [MVP-Outlook]" <su****@outlook code.com> wrote in message
news:ew******** ******@TK2MSFTN GP10.phx.gbl...
Did you read the Help topic on the Restrict method? You need to specify what
field to look in as well as what value to look for:

strFind = "[MessageClass] = " & Chr(34) & "IPM.Contac t" & Chr(34)
Set colItems = = myFolder.Items. Restrict(strFin d)

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:ej******** *****@TK2MSFTNG P15.phx.gbl...
Sue,
I'm having trouble with the items.RESTRICT( "IPM.Contac t") code... how do
you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlook code.com> wrote in message
news:O%******** ********@TK2MSF TNGP12.phx.gbl. ..
And the reason that it happens is that a folder is not required to contain
homogeneous items. Contacts folders in particular usually also contain
distribution lists. You may want to use Items.Restrict to get only items
where the message class is IPM.Contact.
"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:Or******** ******@TK2MSFTN GP09.phx.gbl...
Hi,

I get the same error with one of the items in my contacts. That
is
the reason for the try catch block in my sample.

Ken
------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oIte ms.GetNext, outlook.Contact Item)
Any other thoughts?
Chris Thunell
ct******@pierce associates.com

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
Hi,

Here is a sample console app.

Imports System.Reflecti on

Imports Outlook = Microsoft.Offic e.Interop.Outlo ok

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Applica tion = New Outlook.Applica tion

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpa ce = oApp.GetNamespa ce("mapi")

oNS.Logon("Your ProfileName", Missing.Value, False, True) ' TODO:

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFol der =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderContac ts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLi ne("Total : " & oItems.Count)

Console.WriteLi ne("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.Contact Item

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oIte ms.Item(i), Outlook.Contact Item)

Console.WriteLi ne(i)

Console.WriteLi ne(oContact.Ful lName)

Catch

End Try

Console.WriteLi ne("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:eW******** ******@TK2MSFTN GP10.phx.gbl...
I'm trying to loop through an exchange public folder contact list, get
some
information out of each item, and then put it into a vb.net datatable.
I
run though the code and all works fine until i get to item 250 i get a
"system.inv alid cast exception" and "specified cast is not valid". Once
i
get this error and try to go to the next record.. every record after
gets
the same error... It's like the com connection to outlook has been lost.
(as a side note, the first time i ran this... it worked all the way
through... i'm not sure what i've done to break it, or if this is an
intermittent problem). You can see by my sample below that i've rem'ed
out
different things i've tried.

Any thoughts?
Chris Thunell
ct******@pierce associates.com

'outlook connection settings
'Dim oApp As outlook.Applica tion = New outlook.Applica tion
Dim oApp As Object = New outlook.Applica tion
'Dim oNS As outlook.NameSpa ce = oApp.GetNamespa ce("MAPI")
Dim oNS As Object = oApp.GetNamespa ce("MAPI")
Dim cContacts As outlook.MAPIFol der = oApp.ActiveExpl orer.CurrentFol der
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.Contact Item
Dim oCT As Object
Dim iCount As Int16
Dim X As Int32
Dim NewRow As DataRow
Dim myRow As DataRow
'check to see if the right public folder has been selected
'let's loop through the outlook contacts
'set the counters
X = 1
iCount = 0
iCount = oItems.Count
DoEvents()
oCT = oItems.GetFirst ()
For X = X To iCount 'Each oCT In oItems
StatusBar1.Text = "Looking at Outlook Record " & X & " of " & iCount &
"."

'find the record in tblCompany
'***** i'm currently getting the error on the following line:
'*****oct.entry ID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tb lCompany.FindBy OutlookEntryID( oCT.EntryID)

'do some more stuff and then loop to the next outlook contact record
oCT = oItems.GetNext
Next
oApp = Nothing
oItems = Nothing
oCT = Nothing



Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
19352
by: Fritz Switzer | last post by:
Can anyone provide a small snippet in C# that pulls out the Contacts in Outlook XP. I've seen a couple of examples in C++ and VB in previous newsgroup posts, but either the originals didn't work or my conversion skills are weak. And if I have to use tlbimp.exe what is the right file to use. Thanks for the help. -- Fritz
3
5745
by: Kurt | last post by:
Hi We are developing an off-the-shelf software suite for a certain business sector. Most of the program is simply a GUI on top of some .mdb files. Its a .net application written in c# One major requirement is "Outlook Integration". The Outlook contact folders are to be used for contact management I want to know what is the standard way to do this these days. Most of the information in MSDN seems rather old I first used the Outlook...
7
3307
by: Anushya | last post by:
Hi How to get the id of a name in contact items in outlook. How to do it thru redemption in .net?? i tried the code below. but it shows the error. pls have a look at the code Microsoft.Office.Interop.Outlook.NameSpace oNs; oNs = Connect.oApplication.GetNamespace("MAPI"); //it shows an error here - invalid cast exception
5
3447
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS 5), but fails on IIS 6 running on a Win2003 server. The web uses Pages derived from a custom class I wrote (which itself derives from Page) to provide some common functionality. The Page_Load handler the failing webpage starts out like this: ...
9
3097
by: George McCullen | last post by:
I have an Outlook 2003 using Exchange Server 2003 Public Contacts Folder containing 20,000 Contacts. I am writing a VB .Net 2003 program that loops through all the contacts in a "for each oCt in oItems" loop. After about 250 contacts, the program gives an InvalidCastException Error on the for each loop. I notice that Outlook's memory keeps increasing (using the task manager) until it reaches around 20,000K. When I run the program a second...
3
6052
by: John | last post by:
Hi I am trying to access outlook contacts folders and delete the contacts that do not contain a certain category value in the categories field. I have written the below code but am stuck with the error on the indicated line. Any help would be appreciated. Thanks Regards
1
4871
by: dcd | last post by:
Hi all I'm using trying to get my app to read in all contacts in the contact folder of Outlook. I'm using the Outlook Security manager to stop the pop up warnings. Outlook version is 2002(10.2627.2625) I can create a contact in Outlook and I can use it to send an email. It's just the getting the contacts list that doesn't work. The thing is it also works on my dev machine but not on the users machine. The code I'm using: SecurityManager =...
1
2199
by: philmarsay | last post by:
I have some code that loops through all contacts in a contact folder (approx 3000), reads some details from each contact and uses that information to perform various functions. However, as the program loops through, reading each contact, it will randomly start throwing exceptions (usually a large batch at a time), therefore fails to read the contact. This is the current code snippet that I am using, although I have tried allsorts. I can't...
2
1597
by: vijayasingh | last post by:
I am trying to take contacts data from out look and Synchronize with our database. I am using .net 1.1 framework. iam using following code. OLCont = MyOLList.GetLast 'CType(item, outlook.ContactItem) Do While TypeName(OLCont) <> "Nothing" OLRow = OLTable.NewRow OLRow(0) = 0 OLRow(1) = IIf(OLCont.FirstName Is Nothing, "", OLCont.FirstName) OLRow(2) =...
0
10996
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10347
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7062
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5730
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4541
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4133
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3171
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.