all groups > vb.net upgrade > december 2003 >
You're in the

vb.net upgrade

group:

Problem searching Outlook Contacts in vb.net


Problem searching Outlook Contacts in vb.net Rooneyh
12/29/2003 11:05:30 AM
vb.net upgrade:
I recentely migrated to vb.net and converted a small program from vb6. I
get a build error with the following error message.

"Expression is of type 'Outlook.Items', which is not a collection type."

The program uses a simple For Each ... Next loop to cycle through my Outlook
Contacts and searched for a specified record as shown below.

Private myContacts As Outlook.Items
Private Contact As Outlook.ContactItem
myOlApp = CreateObject("Outlook.application")
myNameSpace = myOlApp.GetNamespace("MAPI")
myContacts =
myNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).Item
s
SearchLen = Len(txtSearchName.Text)
On Error GoTo SearchError
i = 1
'Cylcle through the records and store contacts matching search text
For Each Contact In myContacts
If UCase(VB.Left(Contacts.LastName, SearchLen)) =
UCase(txtSearchName.Text) Then
FirstNameStore(i) = Contacts.FirstName
.
.
.
i = i + 1
End If
Next Contact

The 'myContacts' variable is the one that triggers the error. It's not a
collection type as required by the For Each loop. The code worked fine in
vb6 but I'm at a total loss on how to correct the problem. Anyone who can
help?


Re: Problem searching Outlook Contacts in vb.net Jay B. Harlow [MVP - Outlook]
12/29/2003 6:24:50 PM
Rooneyh,
You need to cast the Outlook.Items collection to the IEnumerable type, to
get For Each to work.

[quoted text, click to view]

Note however that the Contact folder can contain items other then
Outlook.ContactItem, if you have any distribution lists it will contain
Outlook.DistListItem objects also.

Hope this helps
Jay

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Tom Dacon
12/31/2003 2:29:51 PM
That doesn't match my experience. I'm not having any trouble with 'for each'
over an Outlook.Items "collection". The code below (written in VB.Net
instead of C# - sorry 'bout that) works just fine. I haven't included the
source code for the called routines, but the comments describe what they do.
Why does it seem to work OK in VB.Net, but apparently not in C#? Beats me.
Surely VB.Net isn't silently seeking out the IEnumerable interface all by
itself?

Tom Dacon
Dacon Software Consulting
Dim todayItems As Outlook.Items = Nothing

' Returns an Outlook.Items object containing just today's items from

' the Calendar folder (probably but not necessarily AppointmentItems)

todayItems = Me.GetTodaysAppointments()

If (Not todayItems Is Nothing) Then

Debug.WriteLine(vbCrLf + "Today's appointments:")

For Each obj As Object In todayItems

Dim strAppt As String

' Returns a formatted string dump of certain properties

' of the object, if its type is AppointmentItem

strAppt = DumpCalendarItem(obj)

Debug.WriteLine(strAppt)

Next obj

End If



[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Tom Dacon
12/31/2003 2:34:31 PM
Jeez, this just isn't my day...where did I get the idea that the original
poster was writing in C#? Anyway, the VB.Net code works fine on the VS.Net
2003 and the 1.1 framework.

Tom Dacon
Dacon Software Consulting

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Jay B. Harlow [MVP - Outlook]
12/31/2003 5:07:56 PM
Tom,
Are you using the Outlook 2003 PIA?

You are correct, don't need the cast if you are using the Outlook 2003 PIA,
I just verified that.

I was basing my statement on CDO 1.2.1 which does not have a PIA, I had to
cast the collections to IEnumerable to get the For Each to work, hence my
answer. The Outlook 2003 PIA correct states that the Outlook.Items interface
inherits from IEnumerable so you do not need the cast.

Unfortunately I do not have a machine to verify with the Outlook XP PIA or
using normal interop without a PIA.

Hope this helps
Jay

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Rooneyh
12/31/2003 10:55:19 PM
Jay,

Thanks! Your pointer fixed my problem. Your response also pointed out
another problem. It seems the distribution list item causes another error
when looping through the contact items. Is there a way to check for a
distribution list item to avoid the problem?

Thanks,
Rodd

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Jay B. Harlow [MVP - Outlook]
1/1/2004 10:24:09 AM
Rooneyh,
I normally use an "Object" variable for the For Each, then use TypeOf to
check the object for the specific type.

Something like:

Dim Contact As Outlook.ContactItem
Dim DistList As Outlook.DistListItem

For Each item As Object In myContacts
If TypeOf item Is Outlook.ContactItem Then
Contact = DirectCast(item, Outlook.ContactItem)
ElseIf TypeOf item Is Outlook.DistListItem Then
DistList = DirectCast(item, Outlook.DistListItem)
End If
i = i + 1
Next item

Hope this helps
Jay

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Rooney
1/7/2004 8:01:21 AM
Jay,

The solution you sent worked fine on my non-networked machine but when I
moved it to my netowork machine I get something unexpected when checking to
see if the TypeOf item is Outlook.ContactItem.

The debugger tells me that the item is of type "{System.__ComObject}". It
then fails the check to see if it is a ContactItem and just moves on to the
next item.

Any thoughts?

Rodd

[quoted text, click to view]

Re: Problem searching Outlook Contacts in vb.net Jay B. Harlow [MVP - Outlook]
1/7/2004 6:29:11 PM
Rooney,
What is "it". Just the executable? Did you include the PIA if you are using
the PIA, did you include the generated interop if you are using that?

Is your network machine have the same version of Outlook and VS.NET? Does it
have or not have the same PIA installed?

Generally when using interop, I would highly recommend creating a setup
package to ensure that things are installed correctly, also if possible I
would highly recommend using the PIA, however this limits you on Outlook
versions...

If I did not include it, the following site includes a number of articles on
using Outlook with .NET:

http://www.microeye.com/resources/res_outlookvsnet.htm

Hope this helps
Jay

[quoted text, click to view]
Re: Problem searching Outlook Contacts in vb.net Rooney
1/8/2004 7:45:17 AM
Jay,
Yes. "it" is the executable. Specifically, when checking "If TypeOf
Contact Is Outlook.ContactItem Then" to see if the TypeOf item is a
Outlook.ContactItem. Looking at "Contact" in the debugger window, it tells
me that "Contact" is of type "{System.__ComObject}". The "If" check then
fails and simply moves on to the next item in myContacts. Here is the code.

Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myContacts As Object
Dim Contact As Object
Dim Contacts As Outlook.ContactItem
Dim fldFolder As Outlook.MAPIFolder
Dim i As Short
Dim SearchLen As Short

myOlApp = New Outlook.Application
myNameSpace = myOlApp.GetNamespace("MAPI")
fldFolder =
myNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
myContacts = fldFolder.Items
SearchLen = Len(txtSearchName.Text)
On Error GoTo SearchError
If optLastName.Checked = True Then
i = 1
myContacts.Sort("[lastname]", 6)
'Cylcle through the records and store contacts matching search text
For Each Contact In myContacts
If TypeOf Contact Is Outlook.ContactItem Then ' *** Here is the
line I'm referring to ***
Contacts = DirectCast(Contact, Outlook.ContactItem)
If UCase(VB.Left(Contacts.LastName, SearchLen)) =
UCase(txtSearchName.Text) Then
FirstNameStore(i) = Contacts.FirstName
LastNameStore(i) = Contacts.LastName
CompanyStore(i) = Contacts.CompanyName
AddressStore(i) = Contacts.BusinessAddressStreet
CityStore(i) = Contacts.BusinessAddressCity
StateStore(i) = Contacts.BusinessAddressState
ZipStore(i) = Contacts.BusinessAddressPostalCode
PhoneStore(i) = Contacts.BusinessTelephoneNumber
FaxStore(i) = Contacts.BusinessFaxNumber
i = i + 1
End If
End If
Next Contact
End If

I am using Office XP on my home machine (where the code runs fine) and Offic
e 2000 SR-1 on my office (networked) machine. I have included a reference
to Interop.Outlook so that may be my problem. I'm using the same version of
VS.NET on both. I have created a setup package and the installation runs
fine but right now I'm just running it inside VS.NET. Is there a way to
restructure the code to work with both versions? I tried following the
instructions on "Rebuilding the Outlook Interop Assembly" in the link you
sent me, but the "access privileges" alerady appear to be Public. You're
dealing with a complete novice and I'll be honest, that didn't make much
sense to me and I'm not very familiar with the PIA. Our office is planning
to upgrade to Office XP but it hasen't happened yet.

Any insight would be helpful. Thanks.

Rodd

[quoted text, click to view]
Re: Problem searching Outlook Contacts in vb.net Jay B. Harlow [MVP - Outlook]
1/8/2004 6:41:46 PM
Rooney,
[quoted text, click to view]
It sounds like you actually copied the entire project? Correct? If not I
would recommend it.

[quoted text, click to view]
of
Delete the reference to Outlook on your office machine, being certain to
delete the Interop.Outlook.DLLs also (may need to use Windows Explorer),
then Re-add the reference to Outlook. I suspect because you took the
Interop.Outlook from your home machine, which is newer, on your office
machine, which has an older version of Outlook, something is getting
confused.

Unfortunately I cannot verify the above, as I do not have Office 200 SR-1
loaded on a machine.

[quoted text, click to view]
Using the PIA is really the "best" way to do this, especially if this is an
internal app and you can control the version of Outlook deployed. If your
company is planning on upgrading to Office XP and you need to use VB.NET for
this project, then I would strongly suggest you move up the upgrade to
Office XP & deploy the PIA when you deploy XP!

See links under the "Office XP Primary Interop Assembly" section at
http://www.microeye.com/resources/res_outlookvsnet.htm.

Note: Office 2003 ships with its own PIA, that are optionally installed
under the Office 2003 setup program.

The Interop.Outlook assembly is automatically generated by a tool with
VS.NET, it can have quirks that a PIA, which is hand crafted more then
likely will not have.

Hope this helps
Jay

[quoted text, click to view]
AddThis Social Bookmark Button