Import Company Name from Outlook to a combo box in Excel using VBA

Hello Experts,

I'd like to import all of our contacts (company name) to a combo box via VBA code in Excel.
Who is Participating?
Helen FeddemaConnect With a Mentor Commented:
Here is some code that will get the company names from Outlook, create an array of them, and then paste them to a sheet called Company Names in the current workbook.  You can then use the range of company  names as the row source for your combo box.

If your Office version is older than 2007, replace Folder with MAPIFolder in the declarations.
Public Sub ImportCompanyNames()
'Created by Helen Feddema 27-Sep-2011
'Last modified by Helen Feddema 27-Sep-2011

On Error GoTo ErrorHandler

   Dim appOutlook As New Outlook.Application
   Dim itm As Object
   Dim itmContact As Outlook.ContactItem
   Dim fldContacts As Outlook.Folder
   Dim nms As Outlook.Namespace
   Dim strCompanyName As String
   Dim strCompanyNames() As String
   Dim i As Integer
   Dim intUBound As Integer
   Dim rng As Excel.Range
   Set nms = appOutlook.GetNamespace("MAPI")
   Set fldContacts = nms.GetDefaultFolder(olFolderContacts)
   intUBound = fldContacts.Items.Count
   Debug.Print "No. of contacts: " & intUBound
   ReDim strCompanyNames(intUBound)
   i = 1
   For Each itm In fldContacts.Items
      If itm.Class = olContact Then
         Set itmContact = itm
         strCompanyName = itmContact.CompanyName
         If strCompanyName <> "" Then
            Debug.Print "Using company name: " & strCompanyName
            strCompanyNames(i) = strCompanyName
            i = i + 1
         End If
      End If
   Next itm
   'Paste array to worksheet
   Set rng = Application.Sheets("Company Names").Range("A1")
   For i = 1 To intUBound
      rng.Value = strCompanyNames(i)
      Set rng = Application.ActiveCell.Offset(rowoffset:=i)
   Next i
   Exit Sub

   MsgBox "Error No: " & Err.Number _
      & " in ImportCompanyNames procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

Bax060606Author Commented:
Beautiful, would I be able to easily cross reference other pertinant information easily. ie if I loaded this list to a combo box and someone selected company 'ABC' could I autopopulate other fields such as the contact name email etc.

Keep in mind I'm looking for an easy solution for instance, If there is an index number you are keeping track of in your code so I don't have to write a complicated routine for cross referencing. Otherwise no need to answer as I will go about it the long way.
Bax060606Author Commented:
No Comments were added to the code
Helen FeddemaCommented:
You could expand the array to store as many fields as you wish, and then they would be available for selection in the combo box.
Helen FeddemaCommented:
If you wish, you could post another question, mentioning which fields you want to store, and I could do some modified code.
All Courses

From novice to tech pro — start learning today.