[Solved] Outlook DistributionList creation lazarus / 34-Windows ---------------------------------------------------------------------- drschwarcz · 2014-09-05 19:21 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. ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-05 19:51 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 Quote2So, 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'; ---------------------------------------------------------------------- drschwarcz · 2014-09-05 20:49 THX, you are right!  ...and I'm a bit silly.  THX again for quick answer. This is solved. ps: ... .FirstName has changed. ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-05 21:19 Glad to know it works :-)  I've not yet used Lazarus for COM. ---------------------------------------------------------------------- drschwarcz · 2014-09-05 22:16 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. ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-20 10:08 Quote from: drschwarcz on September 06, 2014, 12:16:53 amWhat 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. Quote from: drschwarcz on September 06, 2014, 12:16:53 amOne 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 ---------------------------------------------------------------------- marcov · 2014-09-20 12:14 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 · 2014-09-20 12:32 Quote from: marcov on September 20, 2014, 02:14:13 pmDoes 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... Quote from: marcov on September 20, 2014, 02:14:13 pmI 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. ---------------------------------------------------------------------- marcov · 2014-09-20 13:26 Quote from: Mike.Cornflake on September 20, 2014, 02:32:24 pmYeah, 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. QuoteNot 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 · 2014-09-21 10:01 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; ---------------------------------------------------------------------- drschwarcz · 2014-09-30 18:27 Tested with Win7 32/64bit + Outlook 2007 and 2013, Win8.1 64bit + Outlook 2013