Delete OL Contact using Late Binding (1 Viewer)

MackMan

Registered User.
Local time
Today, 02:26
Joined
Nov 25, 2014
Messages
174
Hi guys. I'm using the following code (with reference to MS Outlook object library) to delete a contact from OL.

Code:
Function DELOUTLOOKCTCT()

On Error GoTo Err_Ctrl
Dim olAppl As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim olCont As Outlook.ContactItem
Dim strContact As String

    '****** Set Criteria for DELETION here **
5    strContact = Nz(Me.PayeeName, vbNullString)
    '*********************************
10  Set olAppl = Outlook.Application
15  Set objNamespace = olAppl.GetNamespace("MAPI")
20  Set objFolder = objNamespace.GetDefaultFolder(olFolderContacts)

25  For Each olCont In objFolder.Items
30      If olCont.FullName = strContact Then
35          olCont.Delete
        End If
    Next
40  If Me.txtStatus = "DELETE" Then
45      MsgBox " Contact(s) deleted from Outlook.", vbInformation, "Delete Outlook Contact details..."
    End If

Exit_Function:
    strContact = vbNullString
    Exit Function

Err_Ctrl:
    el = Erl
    en = Err.Number
    ed = Err.Description
    errMsgStr = "Del OL Contact Info error."
    ctrlfnctnm = "DELOUTLOOKCTCT"
    Call form_err(en, ed, ctrlfnctnm, el, errMsgStr)
    Resume Exit_Function

End Function

However, I'd like to be able to delete using late binding.

Although I'm ok with References to Libraries, Late binding is new to me. I've been having a look around all day yesterday but I'm not sure about :

How I set the contact item (no Jet)
how to set the contacts folder - I've been using const olcontactsfolder = 10 (is this correct?)

I've managed to get late binding to add the contact and it's sorted. But to delete I've been looking for over a day and found nothing to reference to.

is there an easy way to convert early to late? if so how do you guys do it?


As always, help is much appreciated.
 

MackMan

Registered User.
Local time
Today, 02:26
Joined
Nov 25, 2014
Messages
174
Thanks Minty. Looks like exactly what I need.

I'll let you know how I get on.
 

MackMan

Registered User.
Local time
Today, 02:26
Joined
Nov 25, 2014
Messages
174
That whole link was extremely helpful, and I've already changed a couple of pieces of VBA to reflect that. However...

I still have no idea what I'm referencing to be able to delete a contacts' details from Outlook, like using olcontactsfolder = 10 etc....
 

Minty

AWF VIP
Local time
Today, 02:26
Joined
Jul 26, 2013
Messages
10,371
What code do you currently have that isn't working, what's the error and the line it errors on?
 

MackMan

Registered User.
Local time
Today, 02:26
Joined
Nov 25, 2014
Messages
174
I finally got it to work. I built the code using the library reference and back wrote it. Turns out I was only referencing one Const olfoldercontacts = 10
, and not both which I also needed to include Const olcontactitem = 2


The final code (for anyone who's looking for the same is)

Code:
Private Sub DelOLCont()
Const olcontactitem = 2
Const olfoldercontacts = 10

Dim olApp As Object 'Outlook.Application
Dim objNamespace As Object 'Outlook.NameSpace
Dim objFolder As Object 'Outlook.MAPIFolder
Dim olCont As Object ' Outlook.ContactItem
Dim strContact As String


If isAppThere("Outlook.application") = False Then
        Set olApp = CreateObject("Outlook.Application") ' Outlook is not open, create a new instance
    Else
        Set olApp = GetObject(, "Outlook.Application") ' Outlook is already open--use this method
    End If
    
    '****** Set Criteria for DELETION here ********
    strContact = Nz(Me.PayeeName, vbNullString)
    '****************************************
   Set objNamespace = olApp.GetNamespace("MAPI")
   Set objFolder = objNamespace.GetDefaultFolder(olfoldercontacts)

   For Each olCont In objFolder.Items
       If olCont.customerID = strContact Then
           olCont.Delete
        End If
    Next

End Sub

it may not be pretty, and I've not covered any error trapping as yet, but it works perfectly.

Thanks for you help. As always, much appreciated.
 

Users who are viewing this thread

Top Bottom