Recent

Author Topic: [Solved] Outlook DistributionList creation  (Read 10027 times)

drschwarcz

  • New Member
  • *
  • Posts: 17
[Solved] Outlook DistributionList creation
« on: September 05, 2014, 09:21:01 pm »
Hi!

I would like to create an DistList in MS OutLook.
Unfortunately, I found quite a few matter on the Net, dut I have a code which seem good. However I could not get it to work.  :(
Op. system: Windows 8.1, or Windows 7
The newest Lazarus.

Here is the code:

uses ComObj...

procedure AddAddressInDistributionList;
var

myOlApp, myNameSpace, myContact, myDistList, myMailItem, myRecipients : OleVariant;

begin

// Connect to outlook
try

myOlApp := GetActiveOleObject('Outlook.Application');

except

myOlApp := CreateOleObject('Outlook.Application');

end;

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;

myOlApp := Unassigned;
end;


The error message comes this point: olContactItem.
I'd appreciate any good idea.

Greeting. :)
« Last Edit: September 05, 2014, 10:51:53 pm by drschwarcz »
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Outlook DistributionList creation
« Reply #1 on: September 05, 2014, 09:51:18 pm »
Guessing olContactItem is a constant.   As you're doing this entirely dynamically, you're going to need to know what the constants are by value, not by name.

Hmm, this is no use...
http://msdn.microsoft.com/en-us/library/office/aa210907(v=office.11).aspx

Kicking up VisualBasic editor in my Outlook (add Developer to the Ribbon), then using the immediate window
Code: [Select]
? olContactItemreturns
Quote
2
So, try changing
Code: [Select]
myContact:=myOlApp.CreateItem(olContactItem);to
Code: [Select]
myContact:=myOlApp.CreateItem(2);
And, in anticipation of your next question :-)
Quote
? olMailItem
0

UPDATE:
Also, this needs changing
Code: [Select]
.FirstName:='Delphi';to
Code: [Select]
.FirstName:='Lazarus'; :D
« Last Edit: September 05, 2014, 09:57:18 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: Outlook DistributionList creation
« Reply #2 on: September 05, 2014, 10:49:34 pm »
THX, you are right!  :-[ ...and I'm a bit silly.  :o

THX again for quick answer. This is solved.

ps: ... .FirstName has changed.  :D
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: [Solved] Outlook DistributionList creation
« Reply #3 on: September 05, 2014, 11:19:47 pm »
Glad to know it works :-)  I've not yet used Lazarus for COM.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: [Solved] Outlook DistributionList creation
« Reply #4 on: September 06, 2014, 12:16:53 am »
What do you use, VBA or something else?

One more question: Why 2 and 0? What is the logic behind it?

Ohh, It's been two.  :)
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: [Solved] Outlook DistributionList creation
« Reply #5 on: September 20, 2014, 12:08:53 pm »
What do you use, VBA or something else?

For both Excel and Word I do an awful lot of VBA scripting.  As all of my routines in that world are very specific to Word or Excel, there's no point in extracting that code out to say Lazarus.  For my paid work, I'm actually a report writer for various clients, so most of those routines are geared around taking exports from various reporting packages and modifying the format so it meets specific client requirements.

And by leaving those routines in VBA, I can always tweak them on the spot, without needing to break open a compiler.

One more question: Why 2 and 0? What is the logic behind it?

They're just constants.  In Lazarus (see below), we would call them enumerated types.  The first item is 0, the second 1 and so on. 
Code: [Select]
Type
  Position = (poLeft, poRight, poTop, poBottom);

And by using the constants in VBA, your code becomes more readable.  And in fact, for exactly that reason, in your code above, instead of using 2 directly, we should have declared a const, then used that...
Code: [Select]
Const
  olContactItem = 2;
....
  //Create the new Contact
  myContact:=myOlApp.CreateItem(olContactItem);

See for instance:
http://msdn.microsoft.com/en-us/library/office/aa219371(v=office.11).aspx  (couldn't find a more up to date list)
and
http://wiki.freepascal.org/Enumerated_types
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: [Solved] Outlook DistributionList creation
« Reply #6 on: September 20, 2014, 02:14:13 pm »
Does sb know in which header these are in? A typelib imported from Outlook?

I can't seem to find them in the platform SDK.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: [Solved] Outlook DistributionList creation
« Reply #7 on: September 20, 2014, 02:32:24 pm »
Does sb know in which header these are in? A typelib imported from Outlook?

sb?  Looking at the code provided, there's no type library importing involved.
UPDATE:  Hmmm, I see one or two of the constants were initially found, so I guess there may be a Type Library import after all...

I can't seem to find them in the platform SDK.

Yeah, I had the same problem - I just search MSDN generally.  It's why I ended up going to the VBA immediate window in the first instance...

Not sure the Platform SDK is relevant for Office SDK, might have changed in the last few years though. 
« Last Edit: September 20, 2014, 02:34:52 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: [Solved] Outlook DistributionList creation
« Reply #8 on: September 20, 2014, 03:26:55 pm »
Yeah, I had the same problem - I just search MSDN generally.  It's why I ended up going to the VBA immediate window in the first instance...

That's nice for quick solutions, but mass translating a header is more efficient.

Quote
Not sure the Platform SDK is relevant for Office SDK, might have changed in the last few years though.

I had hoped one of the MAPI headers declared it, not.

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: [Solved] Outlook DistributionList creation
« Reply #9 on: September 21, 2014, 12:01:28 pm »
Hi!

The full solution:

Code: [Select]
var

  ContactsFullName : TStringList;
  ContactsEmail : TStringList;

.
.
.

procedure CreateDistributionList;
const

  olFolderContacts = $0000000A;

var

  myOlApp, myNameSpace, myContact, myDistList, myMailItem, myRecipients, ContactsFolder : OleVariant;
  WStr : WideString;
  i : Word;

begin

//Connect to outlook
  try

    myOlApp := GetActiveOleObject('Outlook.Application');

  except

    myOlApp := CreateOleObject('Outlook.Application');

  end;

  myNameSpace:=myOlApp.GetNameSpace('MAPI');

//Get root contacts folder
  ContactsFolder := myNameSpace.GetDefaultFolder(olFolderContacts);

//Create the Distribution List item
  myDistList:=myOlApp.CreateItem(7);
  myDistList.DLName:='Laz Distribution List';

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

  i := 0;

  while i < ContactsFullName.Count do
    begin

//Create the new Contact
      myContact:=myOlApp.CreateItem(2);

      WStr := ContactsFullName.Strings[i];
      myContact.FullName := WStr;

      WStr := ContactsEmail.Strings[i];
      myContact.Email1Address := WStr;

      myContact.Save;

//A Contact with the following e-mail address
//must exist for the AddMembers method to work
      WStr := ContactsFullName.Strings[i];
      myRecipients.Add(WStr);
      myRecipients.ResolveAll;
      myDistList.AddMembers(myRecipients);

      myContact.Delete;

      Inc(i);

    end;

  myDistList.Save;

  myOlApp := Unassigned;

end;
« Last Edit: September 30, 2014, 07:41:17 am by drschwarcz »
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

drschwarcz

  • New Member
  • *
  • Posts: 17
Re: [Solved] Outlook DistributionList creation
« Reply #10 on: September 30, 2014, 08:27:18 pm »
Tested with Win7 32/64bit + Outlook 2007 and 2013, Win8.1 64bit + Outlook 2013
Lazarus 1.6.4 @ Windows 10 x64

Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe.

Einstein, Albert :) ;)

 

TinyPortal © 2005-2018