Specified Cast is not valid error with find and restrict methods

R

Reb DeWinter

I have been working with Outlook COM Add-ins and have run into issues with
using the Find and Restrict method with a couple of different projects. When
I run the same project from an applicatoin outside of Outlook or from a COM
Add-in, but on a computer not on exchange server, there seems to be no
problem at all. But when I run the same code from within a COM Add-in on a
computer connected into an Exchange Server, I randomly get the "specified cas
is not valid error" when using certain Restrict and Find methods - i.e. the
code will work fine and then suddenly it will hang up and there seems to be
no rhyme or reason related to the contact records the code sticks on. Here
is a snippet of the code that I have most recently seen this error with (the
last line with the * is the line where the error occurs:

Dim MasterItems As Outlook.Items
Dim ListItems As Outlook.Items
Dim ListToCheck As String
Dim myList As Outlook.ContactItem
Dim Criteria As String
Dim RestrictItems As Outlook.Items
Dim ProgressMeter As New fclsProgressMeter

'Added 5/23/05 to limit search items to the proper message class
Dim ConItems As Outlook.Items
Dim ConRestrict As String

Try
'restrict ListItems to contact items and use find method to find
the list requested
'CompanyName field is used to hold the List name and CurList is
passed in sub argument
MasterItems = MasterFolder.Items
ListItems = MasterItems.Restrict("[MessageClass] =
'IPM.Contact.List Form'")
ListToCheck = "[CompanyName] = " & CurList
myList = CType(ListItems.Find(ListToCheck), Outlook.ContactItem)

'Collect criteria from myList form
Criteria = "[" & CType(myList.UserProperties("Criteria").Value,
String) & "] = True"

'Use restrict method to create list
'Added code on 5/23/05 to restrict search to NAESB Contact items
ConItems = MasterItems.Restrict("[MessageClass] =
'IPM.Contact.NAESB Contact Form'")
*RestrictItems = ConItems.Restrict(Criteria)

The problem seems to crop up only when RestrictItems.count is larger i.e.
more than 100 records - I don't seem to get it for the smaller numbers of
records. Is this problem typical of COM Add-ins or is there some code
missing here that could make this code run consistently?
 

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