Set up an Excel Spreadsheet from Outlook Contacts - vs. 2010

Within my Outlook (2010) there are several Categories that have the necessary contact information that I need would like to transfer to a Workbook, with each category having a different tab.

Attached is a workbook that provides a model on how the contact information is listed, preferably.  What is the best way to have the code loop through the contacts and create an Excel category list on different worksheet tabs?

For one Category, there is actually a distribution list that may be easier to use for one of the categories, and is listed in Col A.   However, I'm not sure if the distribution list has all of the data which is needed, or just a more complete e-mail list than what I have manually put together.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Harry LeeCommented:

Instead of trying to get the info using loop to extract data from Outlook, I think a more simple and better approach is to export the Outlook contacts to a CSV file, then from Excel extract the data from the CSV by deleting unwanted columns and rearrange the columns order using vba or manually.
Cook09Author Commented:

Well part of the problem is that some of the fields that are in my Contacts are not in the Export Field.  

Additionally, the categories are all over the place, so it would take time to filter each, manually create a tab, and then copy/paste.  I was looking for something a little more automatic, that would also capture the information needed which is not in the export.
Harry LeeCommented:

I just did an contacts export from my Outlook 2013, I know you are using 2010, I find that all the field you are looking for is in the exported CSV file.

Which field are you looking for, specifically, that is not exported?

Maybe I can help you to develop a VBA macro in Excel to extract the data from Outlook without going to Outlook to click export to get the job done.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Cook09Author Commented:

Well, one thing I found while trying different solutions is to highlight the Customize Heading, drag the mouse about 1/4" and it highlights the entire group. Then all that is needed is to copy and paste it into Excel, which also brings the Headings during the Paste.   I think this is even faster than trying to use code.

Maybe you could assist me with something else I was trying to accomplish, and that is to extract the names within a Distribution List.  Is this even possible?
Harry LeeCommented:

You can use the following code to get the list of members of all Outlook Distribution Lists.

Please remember to enable Microsoft Outlook Object Library in Excel VBA Editor.
     Go to VBA Editor
     Tools Menu
     From the list, find Microsoft Outlook xx.0 Object Library (xx.0 is the library version)
     Check the check box then press OK to go back to VBA Editor.

Sub DistListMembers()
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Dim addrLists As AddressLists
    Set addrLists = objNamespace.Session.AddressLists
    Dim addrList As AddressList
    For Each addrList In addrLists
        If addrList.Name = "All Groups" Then
            Set myAddrList = addrList
            Exit For
        End If
    ActiveSheet.Range("A1") = "Dist List"
    ActiveSheet.Range("B1") = "Memberber Name"
    rw = 2
    For Each Entry In myAddrList.AddressEntries
        If Entry.DisplayType = olDistList Then
            For Each Member In Entry.Members
                        ActiveSheet.Range("A" & rw) = Entry.Name
                        ActiveSheet.Range("B" & rw) = Member.Name
                        rw = rw + 1
        End If
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cook09Author Commented:
I did try the code that you provided, and received notification that it was trying to access the Exchange Server.   However, it did error out on....

For Each Member In Entry.Members

Open in new window

I don't know if there is another command that would work, or is it the security settings?

I did find this code, that will import my Contacts, but the e-mail address is in Exchange Format and not really usable.  I did see that a "DisplayName," could provide the e-mail address, but the syntax is different, and still trying to figure it out.

Sub Import_Contacts()

    'Outlook objects. From Local Address Book
    Dim olApp As Outlook.Application
    Dim olNamespace As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olConItems As Outlook.Items
    Dim olItem As Object
    'Excel objects.
    Dim wbBook As Workbook
    Dim wsSheet As Worksheet
    'Location in the imported contact list.
    Dim lnContactCount As Long
    Dim strDummy As String
    'Turn off screen updating.
    Application.ScreenUpdating = False
    'Initialize the Excel objects.
'    Set wbBook = ThisWorkbook
    Set wbBook = ActiveWorkbook
    Set wsSheet = wbBook.Worksheets(1)
    'Format the target worksheet.
    With wsSheet
        .Cells(1, 1).Value = "City"
        .Cells(1, 2).Value = "State"
        .Cells(1, 3).Value = "Zip Code"
        .Cells(1, 4).Value = "MAC Address"
        .Cells(1, 5).Value = "Name"
        .Cells(1, 6).Value = "Job Title"
        .Cells(1, 7).Value = "E-mail"
        .Cells(1, 8).Value = "Account"
        .Cells(1, 9).Value = "Tel No."
        .Cells(1, 10).Value = "Street"
        .Cells(1, 11).Value = "Email"
        With .Range("A1:K1")
            .Font.Bold = True
            .Font.ColorIndex = 10
            .Font.Size = 11
        End With
    End With
    'Initalize the Outlook variables with the MAPI namespace and the default Outlook folder of the current user.
    Set olApp = New Outlook.Application
    Set olNamespace = olApp.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(10)
    Set olConItems = olFolder.Items
    'Row number to place the new information on; starts at 2 to avoid overwriting the header
    lnContactCount = 2
    'For each contact: if it is a business contact, write out the business info in the Excel worksheet;
    'otherwise, write out the personal info.
    For Each olItem In olConItems
        If TypeName(olItem) = "ContactItem" Then
            With olItem
                If InStr(olItem.CompanyName, strDummy) > 0 Then
                    Cells(lnContactCount, 1).Value = .BusinessAddressCity
                    Cells(lnContactCount, 2).Value = .BusinessAddressState
                    Cells(lnContactCount, 3).Value = .BusinessAddressPostalCode
                    Cells(lnContactCount, 4).Value = .OfficeLocation
                    Cells(lnContactCount, 5).Value = .FullName
                    Cells(lnContactCount, 6).Value = .JobTitle
                    Cells(lnContactCount, 7).Value = .IMAddress
                    Cells(lnContactCount, 8).Value = .Account
                    Cells(lnContactCount, 9).Value = .BusinessTelephoneNumber
                    Cells(lnContactCount, 10).Value = .BusinessAddressStreet
                    Cells(lnContactCount, 11).Value = .DisplayName --- Not Correct Format
                    Cells(lnContactCount, 1) = .FullName
                    Cells(lnContactCount, 2) = .HomeAddressStreet
                    Cells(lnContactCount, 3) = .HomeAddressPostalCode
                    Cells(lnContactCount, 4) = .HomeAddressCity
                    Cells(lnContactCount, 5) = .FullName
                    Cells(lnContactCount, 6) = .IMAddress
                End If
                On Error Resume Next      'The 11 was a 6 - Not sure what difference this makes
                wsSheet.Hyperlinks.Add Anchor:=Cells(lnContactCount, 11), _
                                       Address:="mailto:" & Cells(lnContactCount, 11).Value, _
                                       TextToDisplay:=Cells(lnContactCount, 11).Value
            End With
            lnContactCount = lnContactCount + 1
        End If
    Next olItem
    'Null out the variables.
    Set olItem = Nothing
    Set olConItems = Nothing
    Set olFolder = Nothing
    Set olNamespace = Nothing
    Set olApp = Nothing
    'Sort the rows alphabetically using the CompanyName or FullName as appropriate, and then autofit.
    With wsSheet
        .Range("A2", Cells(2, 11).End(xlDown)).Sort key1:=Range("A1"), order1:=xlAscending
    End With
    'Turn screen updating back on.
    Application.ScreenUpdating = True
    MsgBox "The list has successfully been created!", vbInformation
End Sub

Open in new window

Do you have any suggestions?
Harry LeeCommented:
I don't know why you have problem with
For Each Member In Entry.Members

Open in new window

Maybe it has something to do with which MS Outlook Reference Library you are using.


In the above code you have attached, the line 75 should be

                    Cells(lnContactCount, 11).Value = .Email1DisplayName

Open in new window

Cook09Author Commented:
Made the mistake of not adding the correct name to the Distribution List.  When corrected, it ran fine.
Harry LeeCommented:
I'm glad it works for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.