Outlook VBA People (Contacts) Routines (1 Viewer)

Joe Boatman

New member
Local time
Today, 21:47
Joined
May 30, 2020
Messages
25
Below is a whole module's worth of Outlook and support routines. I've put them here in three modules: modFunctions, modOutlook and modVARS.
The first is a simple way to look at the field headings that Outlook uses for People, Outlook's name for Contacts, and how they are used in VBA code.


Simplest method of getting contacts from Outlook using VBA
Add this to modOutlook
Code:
'29 May 2020
'Simplest method of getting contacts from Outlook. See apContactFetchFromOutlook for comprehensive method
'Requires no references or support functions!
'Press F8 to step through this code and View (menu) Locals window
Private Sub apGetContactFromOutlook()

    Dim sName As String, sEmail As String, sCompany As String, sPhone As String, sMobile As String
    Dim nRetVal As Integer
    Dim olApp As Object, oNameSpace As Object, oContactFolder As Object, oContact As Object
    Const olFolderContacts As Long = 10
    Const olContact As Long = 40

On Error Resume Next

    Set olApp = VBA.GetObject(, "Outlook.Application")  'Assumes Outlook is open else err 429
    Err.Clear
    If olApp Is Nothing Then GoTo ExitRoutine   'Outlook needs to be open!

    Set oNameSpace = olApp.GetNamespace("MAPI")
    Set oContactFolder = oNameSpace.GetDefaultFolder(olFolderContacts)
    nRetVal = oContactFolder.Items.Count
    If nRetVal = 0 Then GoTo ExitRoutine    'No contacts

    For Each oContact In oContactFolder.Items
        With oContact
            sName = .FullName
            sEmail = .Email1Address
            sCompany = .CompanyName 'To see all members, see Locals window while stepping through code!
            sPhone = .BusinessTelephoneNumber
            sMobile = .MobileTelephoneNumber
        End With
    Next

    Set oContact = Nothing
    Set oContactFolder = Nothing
    Set oNameSpace = Nothing
    Set olApp = Nothing

ExitRoutine:
End Sub

I'll post the next section into Fully comprehensive method of putting Outlook contacts into a temporary Access table
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:47
Joined
Oct 29, 2018
Messages
13,185
Hi. Thanks for sharing.
 

Users who are viewing this thread

Top Bottom