Solved

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

Posted on 2011-09-27
5
424 Views
Last Modified: 2012-05-12
Hello Experts,

I'd like to import all of our contacts (company name) to a combo box via VBA code in Excel.
0
Comment
Question by:Bax060606
  • 3
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
Helen_Feddema earned 500 total points
Comment Utility
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
   
ErrorHandlerExit:
   Exit Sub

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

End Sub

Open in new window

0
 

Author Comment

by:Bax060606
Comment Utility
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.
0
 

Author Closing Comment

by:Bax060606
Comment Utility
No Comments were added to the code
0
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
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.
0
 
LVL 31

Expert Comment

by:Helen_Feddema
Comment Utility
If you wish, you could post another question, mentioning which fields you want to store, and I could do some modified code.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now