473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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 3580
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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******@pierceassociates.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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***@bellsouth.net> wrote in message news:Or**************@TK2MSFTNGP09.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******@pierceassociates.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Still get an error (specified cast is not valid - Invalid cast exception) on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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.Contact") code... how do you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlookcode.com> wrote in message
news:O%****************@TK2MSFTNGP12.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***@bellsouth.net> wrote in message
news:Or**************@TK2MSFTNGP09.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******@pierceassociates.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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.Contact" & Chr(34)
Set colItems = = myFolder.Items.Restrict(strFind)

--
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******@pierceassociates.com> wrote in message news:ej*************@TK2MSFTNGP15.phx.gbl...
Sue,
I'm having trouble with the items.RESTRICT("IPM.Contact") code... how do you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlookcode.com> wrote in message
news:O%****************@TK2MSFTNGP12.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***@bellsouth.net> wrote in message
news:Or**************@TK2MSFTNGP09.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******@pierceassociates.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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******@pierceassociates.com

"Sue Mosher [MVP-Outlook]" <su****@outlookcode.com> wrote in message
news:ew**************@TK2MSFTNGP10.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.Contact" & Chr(34)
Set colItems = = myFolder.Items.Restrict(strFind)

--
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******@pierceassociates.com> wrote in message
news:ej*************@TK2MSFTNGP15.phx.gbl...
Sue,
I'm having trouble with the items.RESTRICT("IPM.Contact") code... how do
you
implement it?
Chris

"Sue Mosher [MVP-Outlook]" <su****@outlookcode.com> wrote in message
news:O%****************@TK2MSFTNGP12.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***@bellsouth.net> wrote in message
news:Or**************@TK2MSFTNGP09.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******@pierceassociates.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Still get an error (specified cast is not valid - Invalid cast exception)
on
the following code:
oCT = DirectCast(oItems.GetNext, outlook.ContactItem)
Any other thoughts?
Chris Thunell
ct******@pierceassociates.com

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
Hi,

Here is a sample console app.

Imports System.Reflection

Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1

Sub Main()

' Create Outlook application.

Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace.

Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")

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

' Get Messages collection of Inbox.

Dim oInbox As Outlook.MAPIFolder =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderContacts)

Dim oItems As Outlook.Items = oInbox.Items

Console.WriteLine("Total : " & oItems.Count)

Console.WriteLine("Total Unread : " & oItems.Count)

' Loop each unread message.

Dim oContact As Outlook.ContactItem

Dim i As Integer

For i = 1 To oItems.Count

Try

oContact = DirectCast(oItems.Item(i), Outlook.ContactItem)

Console.WriteLine(i)

Console.WriteLine(oContact.FullName)

Catch

End Try

Console.WriteLine("---------------------------")

Next

' Log off.

oNS.Logoff()

' Clean up.

oApp = Nothing

oNS = Nothing

oItems = Nothing

oContact = Nothing

End Sub

End Module

Ken

-------------------
"Chris Thunell" <ct******@pierceassociates.com> wrote in message
news:eW**************@TK2MSFTNGP10.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.invalid 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******@pierceassociates.com

'outlook connection settings
'Dim oApp As outlook.Application = New outlook.Application
Dim oApp As Object = New outlook.Application
'Dim oNS As outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oNS As Object = oApp.GetNamespace("MAPI")
Dim cContacts As outlook.MAPIFolder = oApp.ActiveExplorer.CurrentFolder
'Dim oItems As outlook.Items = cContacts.Items
Dim oItems As Object = cContacts.Items
' Dim oCT As outlook.ContactItem
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.entryID does not give me anything ... it's like it forgot what
"entryID" is.
myRow = Me.DataSet11.tblCompany.FindByOutlookEntryID(oCT.E ntryID)

'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
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...
3
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...
7
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 ...
5
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...
9
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...
3
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...
1
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...
1
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...
2
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.