Thursday, November 10, 2011

Update contacts/DL in outlook using Jacob

Yesterday i blogged about how to access contacts ( Distribution list as well) using Jacob here. Today i will explain how to create new contacts in outlook.
Its same except few deviations:

Creating contact or DL is fairly simple.

Here we are creating a simple contact. Defining variant(2) ensures that item created is a contact item and not DL.

Dispatch createItem = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(2)).toDispatch(); 

We can add values to different attributes. Here i am just adding FullName. We can add other values as well in similar fashion.
Dispatch.put(createItem, "FullName", "NEW CONTACT");  

Saving the item in outlook.
Dispatch.call(createItem, "Save");


in order to create DL, change required is that we need to use Variant(7),
Dispatch createItem = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(7)).toDispatch(); 


then we can set DL specific properties as below:
Dispatch.put(createItem, "DLName", "NEW DL");  


So we are now done with creating contacts. In case we are creating DL, just creating empty DL wont suffice. We need to add members to it.
There are two approaches which can be followed to add member:

  • one is adding single member
  • or adding bulk members together
Here is the code snippet to add single member in DL:

We are creating a recipient in DL,
Dispatch recepient = Dispatch.call((Dispatch)oNameSpace, "CreateRecipient", "abc@gmail.com").toDispatch(); 

Its mandatory to resolve the recipient otherwise it wont be added in DL. resolving means that the address given should be a valid email address, it might not be an existing address but it should compile to the rules of a valid email address
Dispatch.call(recepient,"Resolve");

Now we are adding recipient just created to the DL and saving it.
Dispatch.call(createItem, "AddMember", recepient);
Dispatch.call(createItem, "Save");

We can also add multiple members together to a DL:

Here we are creating a new Item which will hold a list of all recipients:

Dispatch createItem1 = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(0)).toDispatch(); 
Dispatch recepients = Dispatch.call(createItem1, "Recipients").toDispatch();

Add members to the recipient list:
Dispatch.call(recepients,"Add", "xyz@yahoo.com");

Dispatch.call(recepients,"Add", "abc@gmail.com");


Now as we added multiple members to list, we need to resolve all of them so instead of simple "Resolve" we will use "ResolveAll". it does the same job which resolve does for single item.
Dispatch.call(recepients,"ResolveAll");
Add all members to DL and save it.
Dispatch.call(createItem, "AddMembers", recepients);
Dispatch.call(createItem, "Save");

So this way we can create new items in outlook. Jacob is very powerful tool to access different Microsoft applications and work with them. Dispatch class provides the method to call native vba methods.

Accessing Outlook contacts using JACOB

Here i will show how to access outlook components using JACOB. You can get brief idea of JACOB here.

I have developed the code to access and create contacts:

this code gives the handle of the outlook application.
ActiveXComponent component = new ActiveXComponent("Outlook.Application");
Dispatch oOutlook = component.getObject();



We are creating a session or namespace of the outlook to perform all the operations.
Object oNameSpace = component.getProperty("Session").toDispatch();

each folder in outlook is designated by a particular integer value. We need to provide that integer value in order to access it. Contacts folder has integer value 10 while inbox has integer value 6. Dispatch class internally makes a JNI call in order to execute this operation on MS application.

Dispatch contacts = Dispatch.call((Dispatch) oNameSpace,
"GetDefaultFolder", new Integer(10)).toDispatch();

If translated to VB, above code will look like,
oNameSpace.GetDefaultFolder(10);


So Jacob package just reads the class name, function name and attributes to be passed to function and makes a native call to dll.

Here we are creating an item in Contacts folder. Now Variant(2) tells the type of item to be created. To create a contact variant should be 2 while to create distribution list it should be 7.
Dispatch createItem = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(2)).toDispatch(); 


Here we are just adding name to the contact and saving it in outlook.
Dispatch.put(createItem, "FullName", "NEW CONTACT");
Dispatch.call(createItem, "Save");

Once done you can see the new contact being created in outlook contacts.

Similarly we can create Distribution list in outlook just using Variant value 7 as below:
Dispatch createItem = Dispatch.call((Dispatch)oOutlook, "CreateItem", new Variant(7)).toDispatch(); 


Also as Fullname is not an attribute of Distribution list we can specify the name of the DL and save it in outlook as below:

Dispatch.put(createItem, "DLName", "NEW DL");
Dispatch.call(createItem, "Save");


JACOB - Java COM Bridge

JACOB is a open source developed by sourgeforge.net to access the microsoft applications like outlook, excel etc.

It can be downloaded from jacob-project. In the zip you will find jacob.jar and dll. In order to use it add the jar and dll in libraries.
See how to configure dll for the jar how to add libraries in eclipse.

Once configured all the properties and attributes of microsoft applications can be accessed in java layer.

Wednesday, November 9, 2011

Add libraries in Eclipse

To add libraries in eclipse which are required by the jars, below steps should be followed:

  • Open properties: Project(right-click) -> properties
  • Go to Java Build Path -> Libraries. Add Jars.. to add jar in classpath
  • Expand the jar, there is a field "Native Library Location". Browse and add the folder where library is present.
  • Build the project.
Now jar is able to see the library it needs.