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 =, "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., "Save");

in order to create DL, change required is that we need to use Variant(7),
Dispatch createItem =, "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 =, "CreateRecipient", "").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,"Resolve");

Now we are adding recipient just created to the DL and saving it., "AddMember", recepient);, "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 =, "CreateItem", new Variant(0)).toDispatch(); 
Dispatch recepients =, "Recipients").toDispatch();

Add members to the recipient list:,"Add", "");,"Add", "");

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.,"ResolveAll");
Add all members to DL and save it., "AddMembers", recepients);, "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.

No comments:

Post a Comment