PDA

View Full Version : Populating a ComboBox with contact names



3Peppers
08-27-2015, 07:45 AM
Dear all,

I am relatively new and self-taught, so excuse any ignorance in my question... I searched a little and could not find a thread here that resolved my problem. I am writing some code to copy email signatures into a form, from which I can extract components (full name, phone number etc) by highlighting into fields. Clicking a button creates a new contact with these fields populated... This works well, and has proven quite useful.

My issue lies in the next improvement. I would like a way to check if the contact already exists so that I can then update that contact rather than create a new one. My problem is pretty basic. I have used some code i have found in many forums and posts and always looks nearly identical to load the contacts into a ComboBox... My issue is that it does not work. I get no error - the ComboBox is just blank. I do not know enough to troubleshoot this any further... Is my contacts folder maybe different to the norm?

I am using Outlook 2010.

Here is the extract of the code from the form that is supposed to populate the ComboBox:

Private Sub ComboBox1_Change()


Dim MyOLApp As New Outlook.Application
Dim myNameSpace As NameSpace
Dim myContacts As Items
Dim myContact As contactItem
Dim newfax As MailItem
Set myNameSpace = MyOLApp.GetNamespace("MAPI")
Set myContacts = myNameSpace.GetDefaultFolder(olFolderContacts).Items


ComboBox1.Clear
For Each myContact In myContacts
ComboBox1.AddItem myContact.FullName
ComboBox1.Column(1, ComboBox1.ListCount - 1) = myContact.FullName
Next


Set myContacts = Nothing
Set myNameSpace = Nothing
Set MyOLApp = Nothing


End Sub


I appreciate any and all help, and your patience with a beginner.

Sammy 3Peppers.

gmayor
08-28-2015, 01:48 AM
Assuming a userform combobox, on a form with a combo box a text box and two command buttons then in an ordinary module use the following code. This demonstrates how to add items from contacts to a combo box and write the first column of the selected item to the text box.


Option Explicit

Sub ShowMyForm()
Dim olNS As NameSpace
Dim olFolder As MAPIFolder
Dim olContact As ContactItem
Dim oFrm As New UserForm1
Dim strItem As String
Dim i As Long
Set olNS = GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(olFolderContacts)
On Error Resume Next
With oFrm
With .ComboBox1
i = 1
.AddItem "[Select Contact]"
For Each olContact In olFolder.Items
strItem = olContact.FileAs
strItem = Replace(strItem, vbCrLf, Chr(32) & Chr(47) & Chr(32))
.AddItem
.List(i, 0) = strItem
.List(i, 1) = olContact.FirstName
.List(i, 2) = olContact.LastName
i = i + 1
Next olContact
.ListIndex = 0
End With
.Show
If .Tag = 0 Then GoTo lbl_Exit
'do something with the values from the form.
End With
lbl_Exit:
Unload oFrm
Set olNS = Nothing
Set olFolder = Nothing
Set olContact = Nothing
Exit Sub
End Sub
and in the userform code

Option Explicit

Private Sub ComboBox1_Change()
If Me.ComboBox1.ListIndex > 0 Then
Me.TextBox1.Text = Me.ComboBox1.Column(0)
Else
Me.TextBox1.Text = ""
End If
End Sub

Private Sub CommandButton1_Click()
Me.Hide
Me.Tag = 1
End Sub

Private Sub CommandButton2_Click()
Me.Hide
Me.Tag = 0
End Sub