0

I am using jacob library. Using jacob library and following this tutorial i am able to add a contact in outlook. Now i want to delete and update that contact using jacob. i want to know is there any way to delete the outlook contact using jacob.

I am using this code to add contact in outlook. here email id is unique id.

        ActiveXComponent axOutlook = new ActiveXComponent("Outlook.Application");
        Dispatch oOutlook = axOutlook.getObject();
        Dispatch createContact = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(2)).toDispatch();

        Dispatch.put(createContact,"LastName",cont.getLastName());
        Dispatch.put(createContact,"FirstName",cont.getFirstName());
        Dispatch.put(createContact,"Title",cont.getTitle());
        Dispatch.put(createContact,"Email1Address",cont.getPrimaryEmail());

        Dispatch.call(createContact, "Save");
Waqas Ali
  • 1,642
  • 4
  • 32
  • 55

1 Answers1

1

JACOB is a very thin wrapper around COM IDispatch calls, so if you want to know how to do any particular task in Outlook the starting point would be the official Outlook Object Model documentation

Your particular case, locating and deleting a contact, is performed through

namespace = outlookApplication.GetNamespace("MAPI")
contactsFolder = namespace.GetDefaultFolder(olFolderContacts)
contact = contactsFolder.items.find( "[Email1Address] = 'mail@server.com' )

if (contact != null)
{
    contact.Delete
}

The second half of the work is translating these calls to JACOB-speak. Assuming you have located your contact item, the code would be something like

ActiveXComponent outlookApplication = new ActiveXComponent("Outlook.Application");
Dispatch namespace = outlookApplication.getProperty("Session").toDispatch();

Dispatch contactsFolder = Dispatch.call(namespace, "GetDefaultFolder", new Integer(10)).toDispatch();
Dispatch contactItems = Dispatch.get(contactsFolder, "items");
String filter = String.format("[Email1Address] = '%s'", cont.getPrimaryEmail());
Dispatch contact = Dispatch.call(contactItems, "find", filter);

if (contact != null)
{
    Dispatch.call(contactItem, "Delete");
}
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
  • can you tell me how to locate an contact from outlook using jacob?? – Waqas Ali Jun 28 '13 at 12:17
  • If you extend your question with exactly HOW (name? some other unique property? did you store the entryID when you added the contact?) you want to locate your contact, preferably with OOM code, I'll gladly add the additional information to this answer. – Paul-Jan Jun 28 '13 at 12:29
  • Thanks you are the only one who solved my problem. thanks again – Waqas Ali Jul 02 '13 at 07:40