Link to home
Start Free TrialLog in
Avatar of Delphi-Lover
Delphi-Lover

asked on

Outlook, how to add an address in a distribution list

Hello,

I've tried to add a new mailaddress in an addresslist, but I got an error when doing so. I did not find an other way to add an address.

Thanks a lot,

Delphi-Lover.

Var obj, Entry, NewMember : OleVariant;
begin
  obj := CreateOleObject('outlook.application');
  obj := obj.GetNameSpace('MAPI');

  //Create an new distribution list
  //AddressList(6) is my personal addressbook
  Entry:=obj.AddressLists(6).AddressEntries.Add('MAPIPDL','New List');
  Entry.Update;

  NewMember:=Entry.members;
  //Error Here:
  NewMember.Add('SMTP','NewName');
End;
Avatar of Delphi-Lover
Delphi-Lover

ASKER

It seems that I have to answer my own question...

To add a contact in a DistributionList you can use an AddMember function. The only disadvantage is that the contact added must be in your contactlist already. So the following code creates first the contact and then add the contact to the DistributionList. I don't need the contact after, so at the end I just delete the contact. (It seems the addmember makes a COPY of the contact in the DistributionList)

Greetings,

Delphi-Lover.

uses  ComObj;

procedure AddAddressInDistributionList;
Var myOlApp,
    myNameSpace,
    myContact,
    myDistList,
    myMailItem,
    myRecipients : OleVariant;
begin
   myOlApp:=CreateOleObject('outlook.application');
   myNameSpace:=myOlApp.GetNameSpace('MAPI');

   //Create the new Contact
   myContact:=myOlApp.CreateItem(olContactItem);
   myContact.FullName:='New Name';
   myContact.Email1Address:='username@domain.com';
   myContact.Save;
   {
   .FirstName:='Delphi';
   .LastName:='Lover';
   .MobileTelephoneNumber:='123456');
   .HomeAddressStreet:='Delphi Lane 9';
   .HomeAddressCity:='Amsterdam';
   .HomeAddressState:='NL';
   .HomeAddressPostalCode:='1968';
   .Categories:='Business,Personal';
   //More field availble!!
   myContact.Display;
   }

   //Create the Distribution List item
   //olDistributionListItem = 7;
   //this constant is not in my Outlook API...
   myDistList:=myOlApp.CreateItem(7);
   myDistList.DLName:='Test Distribution List';

   //The MailItem is required to
   //create the Recipients collection
   myMailItem:=myOlApp.CreateItem(olMailItem);
   myRecipients:=myMailItem.Recipients;

   //A Contact with the following e-mail address
   //must exist for the AddMembers method to work
   myRecipients.Add('username@domain.com');
   myRecipients.ResolveAll;
   myDistList.AddMembers(myRecipients);
   myDistList.Save;
   //myDistList.Display;

   myContact.Delete;
end;
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial