Is there an alternate, faster method of searching large numbers of contacts

  • Thread starter Thread starter Lucian Sitwell
  • Start date Start date
L

Lucian Sitwell

I've got a user defined dialog box to call up addresses like the
addressbook, but will allow me to insert the other addresses, not just the
mailing address.

Based on the way many others have done this, I used an array, but I have
about 1500 contacts and it takes a long time to load.

Is there:
1. a way of only having to go through the pain of waiting only at
the start--- I notice that the built-in
address book in word takes quite a while to load my outlook contacts
the first time, but is fast after that.

2. another approach that would be faster.

Lucian
 
Michael,

My approach was as follows:

Dim myContactArray As Variant, intIndex As Integer

Private Sub cboContact_Change()
intIndex = cboContact.ListIndex
txtContactAddress.Text = Mid(myContactArray(intIndex),
InStr(myContactArray(intIndex), vbCrLf) + 2)
End Sub

Private Sub cmdCancel_Click()
Unload frmMyAddBook
End Sub

Private Sub UserForm_Initialize()

' Form to bring up Outlook contacts and insert either business, home or
' other address information. Uses QuickSort

Dim olapp As New Outlook.Application
Dim nspNameSpace As Outlook.NameSpace
Dim fldContacts As Outlook.MAPIFolder
Dim objContacts As Object
Dim objContact As Object
Dim myCount As Integer
Dim objItem As ContactItem
Dim strZLS As String
Dim intCurrentContact As Integer

On Error GoTo ErrorHandler

Set olapp = Outlook.Application
Set nspNameSpace = olapp.GetNamespace("MAPI")
Set fldContacts = nspNameSpace.GetDefaultFolder(olFolderContacts)
Set objContacts = fldContacts.Items.Restrict("[BusinessAddress]<> '" & _
strZLS & "'")

ReDim myContactArray(objContacts.Count - 1)
For Each objContact In objContacts
myContactArray(myCount) = objContact.FullName & vbCrLf &
objContact.BusinessAddress
myCount = myCount + 1
Next objContact
QuickSort myContactArray

cboContact.List = myContactArray
cboContact.ListIndex = 0
intIndex = cboContact.ListIndex


ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & vbCr & "Help File: " & Err.HelpFile & vbCr
& _
"Help Context: " & Err.HelpContext & vbCr & _
"Error: " & Err.Number & vbCr & _
"Source: " & Err.Source, vbMsgBoxHelpButton, vbOKOnly
End If

End Sub

Public Sub QuickSort(ByRef vntArr As Variant, _
Optional ByVal lngLeft As Long = -2, _
Optional ByVal lngRight As Long = -2)

Dim I As Long
Dim j As Long
Dim lngMid As Long
Dim vntTestVal As Variant

If lngLeft = -2 Then lngLeft = LBound(vntArr)
If lngRight = -2 Then lngRight = UBound(vntArr)

If lngLeft < lngRight Then
lngMid = (lngLeft + lngRight) \ 2
vntTestVal = vntArr(lngMid)
I = lngLeft
j = lngRight
Do
Do While vntArr(I) < vntTestVal
I = I + 1
Loop
Do While vntArr(j) > vntTestVal
j = j - 1
Loop
If I <= j Then
Call SwapElements(vntArr, I, j)
I = I + 1
j = j - 1
End If
Loop Until I > j

' Optimize sort by sorting smaller segment first
If j <= lngMid Then
Call QuickSort(vntArr, lngLeft, j)
Call QuickSort(vntArr, I, lngRight)
Else
Call QuickSort(vntArr, I, lngRight)
Call QuickSort(vntArr, lngLeft, j)
End If
End If
End Sub


' Used in QuickSort function
Private Sub SwapElements(ByRef vntItems As Variant, _
ByVal lngItem1 As Long, _
ByVal lngItem2 As Long)

Dim vntTemp As Variant

vntTemp = vntItems(lngItem2)
vntItems(lngItem2) = vntItems(lngItem1)
vntItems(lngItem1) = vntTemp
End Sub
 
Hi Lucian,

here is a sample for CDO:

Dim oSess As MAPI.Session
Dim oMsgs As MAPI.Messages
Dim oMsg As MAPI.Message
Dim oFld As MAPI.Folder
Dim oFields As MAPI.Fields
Dim myCount As Long
Const CdoPropSetID3 As String = "0420060000000000C000000000000046"
Const CdoContact_BusinessAddress = "{" & CdoPropSetID3 & "}" &
"0x801B"
Const CdoPR_DISPLAY_NAME As Long = &H3001001F

Set oSess = New MAPI.Session
oSess.LogOn , , False, False, , True
Set oFld = oSess.GetDefaultFolder(CdoDefaultFolderContacts)
Set oFld = oFld.Folders("test")

Set oMsgs = oFld.Messages
ReDim myContactArray(oMsgs.Count - 1)
For Each oMsg In oMsgs
Set oFields = oMsg.Fields
If Len(oFields(CdoContact_BusinessAddress)) Then
myContactArray(myCount) = oFields(CdoPR_DISPLAY_NAME) & vbCrLf &
oFields(CdoContact_BusinessAddress)
End If
myCount = myCount + 1
Next


--
Viele Grüße
Michael Bauer


Lucian Sitwell said:
Michael,

My approach was as follows:

Dim myContactArray As Variant, intIndex As Integer

Private Sub cboContact_Change()
intIndex = cboContact.ListIndex
txtContactAddress.Text = Mid(myContactArray(intIndex),
InStr(myContactArray(intIndex), vbCrLf) + 2)
End Sub

Private Sub cmdCancel_Click()
Unload frmMyAddBook
End Sub

Private Sub UserForm_Initialize()

' Form to bring up Outlook contacts and insert either business, home or
' other address information. Uses QuickSort

Dim olapp As New Outlook.Application
Dim nspNameSpace As Outlook.NameSpace
Dim fldContacts As Outlook.MAPIFolder
Dim objContacts As Object
Dim objContact As Object
Dim myCount As Integer
Dim objItem As ContactItem
Dim strZLS As String
Dim intCurrentContact As Integer

On Error GoTo ErrorHandler

Set olapp = Outlook.Application
Set nspNameSpace = olapp.GetNamespace("MAPI")
Set fldContacts = nspNameSpace.GetDefaultFolder(olFolderContacts)
Set objContacts = fldContacts.Items.Restrict("[BusinessAddress]<> '" & _
strZLS & "'")

ReDim myContactArray(objContacts.Count - 1)
For Each objContact In objContacts
myContactArray(myCount) = objContact.FullName & vbCrLf &
objContact.BusinessAddress
myCount = myCount + 1
Next objContact
QuickSort myContactArray

cboContact.List = myContactArray
cboContact.ListIndex = 0
intIndex = cboContact.ListIndex


ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & vbCr & "Help File: " & Err.HelpFile & vbCr
& _
"Help Context: " & Err.HelpContext & vbCr & _
"Error: " & Err.Number & vbCr & _
"Source: " & Err.Source, vbMsgBoxHelpButton, vbOKOnly
End If

End Sub

Public Sub QuickSort(ByRef vntArr As Variant, _
Optional ByVal lngLeft As Long = -2, _
Optional ByVal lngRight As Long = -2)

Dim I As Long
Dim j As Long
Dim lngMid As Long
Dim vntTestVal As Variant

If lngLeft = -2 Then lngLeft = LBound(vntArr)
If lngRight = -2 Then lngRight = UBound(vntArr)

If lngLeft < lngRight Then
lngMid = (lngLeft + lngRight) \ 2
vntTestVal = vntArr(lngMid)
I = lngLeft
j = lngRight
Do
Do While vntArr(I) < vntTestVal
I = I + 1
Loop
Do While vntArr(j) > vntTestVal
j = j - 1
Loop
If I <= j Then
Call SwapElements(vntArr, I, j)
I = I + 1
j = j - 1
End If
Loop Until I > j

' Optimize sort by sorting smaller segment first
If j <= lngMid Then
Call QuickSort(vntArr, lngLeft, j)
Call QuickSort(vntArr, I, lngRight)
Else
Call QuickSort(vntArr, I, lngRight)
Call QuickSort(vntArr, lngLeft, j)
End If
End If
End Sub


' Used in QuickSort function
Private Sub SwapElements(ByRef vntItems As Variant, _
ByVal lngItem1 As Long, _
ByVal lngItem2 As Long)

Dim vntTemp As Variant

vntTemp = vntItems(lngItem2)
vntItems(lngItem2) = vntItems(lngItem1)
vntItems(lngItem1) = vntTemp
End Sub







Michael Bauer said:
Hi Lucian,

please show us your current approach.

--
Viele Grüße
Michael Bauer


just
the I
have outlook
contacts
 
Michael,

I am just a beginner in this and I just got lost. I don't know much about
CDO other than what it basically is. Can you refer me to a book or site
that might be able to help?

Lucian


Michael Bauer said:
Hi Lucian,

here is a sample for CDO:

Dim oSess As MAPI.Session
Dim oMsgs As MAPI.Messages
Dim oMsg As MAPI.Message
Dim oFld As MAPI.Folder
Dim oFields As MAPI.Fields
Dim myCount As Long
Const CdoPropSetID3 As String = "0420060000000000C000000000000046"
Const CdoContact_BusinessAddress = "{" & CdoPropSetID3 & "}" &
"0x801B"
Const CdoPR_DISPLAY_NAME As Long = &H3001001F

Set oSess = New MAPI.Session
oSess.LogOn , , False, False, , True
Set oFld = oSess.GetDefaultFolder(CdoDefaultFolderContacts)
Set oFld = oFld.Folders("test")

Set oMsgs = oFld.Messages
ReDim myContactArray(oMsgs.Count - 1)
For Each oMsg In oMsgs
Set oFields = oMsg.Fields
If Len(oFields(CdoContact_BusinessAddress)) Then
myContactArray(myCount) = oFields(CdoPR_DISPLAY_NAME) & vbCrLf &
oFields(CdoContact_BusinessAddress)
End If
myCount = myCount + 1
Next


--
Viele Grüße
Michael Bauer


Lucian Sitwell said:
Michael,

My approach was as follows:

Dim myContactArray As Variant, intIndex As Integer

Private Sub cboContact_Change()
intIndex = cboContact.ListIndex
txtContactAddress.Text = Mid(myContactArray(intIndex),
InStr(myContactArray(intIndex), vbCrLf) + 2)
End Sub

Private Sub cmdCancel_Click()
Unload frmMyAddBook
End Sub

Private Sub UserForm_Initialize()

' Form to bring up Outlook contacts and insert either business, home or
' other address information. Uses QuickSort

Dim olapp As New Outlook.Application
Dim nspNameSpace As Outlook.NameSpace
Dim fldContacts As Outlook.MAPIFolder
Dim objContacts As Object
Dim objContact As Object
Dim myCount As Integer
Dim objItem As ContactItem
Dim strZLS As String
Dim intCurrentContact As Integer

On Error GoTo ErrorHandler

Set olapp = Outlook.Application
Set nspNameSpace = olapp.GetNamespace("MAPI")
Set fldContacts = nspNameSpace.GetDefaultFolder(olFolderContacts)
Set objContacts = fldContacts.Items.Restrict("[BusinessAddress]<> '" & _
strZLS & "'")

ReDim myContactArray(objContacts.Count - 1)
For Each objContact In objContacts
myContactArray(myCount) = objContact.FullName & vbCrLf &
objContact.BusinessAddress
myCount = myCount + 1
Next objContact
QuickSort myContactArray

cboContact.List = myContactArray
cboContact.ListIndex = 0
intIndex = cboContact.ListIndex


ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Description & vbCr & "Help File: " & Err.HelpFile & vbCr
& _
"Help Context: " & Err.HelpContext & vbCr & _
"Error: " & Err.Number & vbCr & _
"Source: " & Err.Source, vbMsgBoxHelpButton, vbOKOnly
End If

End Sub

Public Sub QuickSort(ByRef vntArr As Variant, _
Optional ByVal lngLeft As Long = -2, _
Optional ByVal lngRight As Long = -2)

Dim I As Long
Dim j As Long
Dim lngMid As Long
Dim vntTestVal As Variant

If lngLeft = -2 Then lngLeft = LBound(vntArr)
If lngRight = -2 Then lngRight = UBound(vntArr)

If lngLeft < lngRight Then
lngMid = (lngLeft + lngRight) \ 2
vntTestVal = vntArr(lngMid)
I = lngLeft
j = lngRight
Do
Do While vntArr(I) < vntTestVal
I = I + 1
Loop
Do While vntArr(j) > vntTestVal
j = j - 1
Loop
If I <= j Then
Call SwapElements(vntArr, I, j)
I = I + 1
j = j - 1
End If
Loop Until I > j

' Optimize sort by sorting smaller segment first
If j <= lngMid Then
Call QuickSort(vntArr, lngLeft, j)
Call QuickSort(vntArr, I, lngRight)
Else
Call QuickSort(vntArr, I, lngRight)
Call QuickSort(vntArr, lngLeft, j)
End If
End If
End Sub


' Used in QuickSort function
Private Sub SwapElements(ByRef vntItems As Variant, _
ByVal lngItem1 As Long, _
ByVal lngItem2 As Long)

Dim vntTemp As Variant

vntTemp = vntItems(lngItem2)
vntItems(lngItem2) = vntItems(lngItem1)
vntItems(lngItem1) = vntTemp
End Sub







Michael Bauer said:
Hi Lucian,

please show us your current approach.

--
Viele Grüße
Michael Bauer


I've got a user defined dialog box to call up addresses like the
addressbook, but will allow me to insert the other addresses, not just
the
mailing address.

Based on the way many others have done this, I used an array, but I
have
about 1500 contacts and it takes a long time to load.

Is there:
1. a way of only having to go through the pain of waiting
only at
the start--- I notice that the built-in
address book in word takes quite a while to load my outlook
contacts
the first time, but is fast after that.

2. another approach that would be faster.

Lucian
 
Back
Top