Find and Modify an Outllook Contact

peterhw

New Member
Joined
Jan 23, 2012
Messages
39
I am trying to build a module to add notes etc to an outlook contact.
I have tried a number of things and the code below works.
Lines commented out generated errors.
Why does
Code:
Dim olApp As Outlook.Application
fail when so many examples use it?

How can I specify different folder for the contacts?

Code:
Sub GetContact()
Dim int1 as integer
Dim str1 As String, str2 As String
    'Dim olApp As Outlook.Application
    'Dim olNs As Namespace
    'Dim Fldr As MAPIFolder
    'Dim olCi As ContactItem
Dim olApp As Object
'On Error GoTo Error_Handler  '''''' need to add code for this top work
    Const olContactItem = 2
    Const olFolderContacts = 10
    Set olApp = CreateObject("Outlook.Application")
    Set olContact = olApp.CreateItem(olContactItem)
    'Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.Folders("Personal Folders").Folders("Contacts")
    int1 = 1
    For Each olCi In Fldr.Items
        str1 = olCi.CompanyName
        Sheets(ActiveSheet.Name).Cells(int1, 1) = str1
        str2 = olCi.FullName
        Sheets(ActiveSheet.Name).Cells(int1, 2) = str2
        int1 = int1 + 1
    Next olCi


    Set olCi = Nothing
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing


End Sub

any help appreciated
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The line mentioned and the rest of your variables are generating errors because you need to set a reference to the Outlook Object Library. To do this click on Tools>References in the Visual Basic Editor, then choose the reference called "Microsoft Outlook x.0 Object Library" where x will represent the latest version of Outlook that you have installed. The rest of your code looks like you've mashed together 2 or more procedures that do two different things and use two different approaches. Below I've taken out one set of code for you to try and run

Code:
Sub GetContact()Dim int1 As Integer
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olCi As Outlook.ContactItem
    
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.Folders("Personal Folders").Folders("Contacts")
    
    int1 = 1
    For Each olCi In Fldr.Items
        Cells(int1, 1) = olCi.CompanyName
        Cells(int1, 2) = olCi.FullName
        int1 = int1 + 1
    Next olCi
    
    Set olCi = Nothing
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
End Sub

Hope this helps

Simon
 
Upvote 0
The line ......... "Microsoft Outlook x.0 Object Library" where x will represent the latest version of Outlook that you have installed.

Never looked at this before but seems to solve that problem x = 12.0.
The code works fine until I get to a GROUP ITEM rather than a CONTACT ITEM.
So include the extra lines below - commented out - for the moment. If the ' - comment is removed the code fails line 1 - error 13 type mismatch.
I believe I have managed to get the code working elsewhere using the TypeName (as below)

Code:
Sub GetContact()Dim int1 As Integer
Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olCi As Outlook.ContactItem
    
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.Folders("Personal Folders").Folders("Contacts")
    Set Fldr = Fldr.Folders("IMS")                       '  this works too
    int1 = 1
    For Each olCi In Fldr.Items
        'If TypeName(olCi) = "Contactitem" Then        '   <<<<<<<<<<<<<<<<<<
            Cells(int1, 1) = olCi.CompanyName
            Cells(int1, 2) = olCi.FullName
            int1 = int1 + 1
        'End If                                          '   <<<<<<<<<<<<<<<<<<
    Next olCi
    Set olCi = Nothing
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
End Sub

Thanks again
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,122
Members
451,399
Latest member
alchavar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top