[Solved]How can I add a string to Variant type? lazarus / 34-Windows ---------------------------------------------------------------------- drschwarcz · 2014-09-20 09:37 Hello! Op.system: Windows 8.1 and the newest Lazarus. I want to create a new contact to Outlook. The code is working when I type the strint to the code. Like that: myContact.FullName := 'Full name'; But is not worknig when I want give from a String type (TStringList). myContact.FullName := ContactsFullName.Strings[1]; My code: procedure AddAddressInDistributionList; const olFolderContacts = $0000000A; var myOlApp, myNameSpace, myContact : OleVariant; begin //Connect to outlook myOlApp := CreateOleObject('Outlook.Application'); myNameSpace:=myOlApp.GetNameSpace('MAPI'); //Get root contacts folder ContactsFolder := myNameSpace.GetDefaultFolder(olFolderContacts); //Create the new Contact myContact:=myOlApp.CreateItem(ContactsFolder.Folders.Count); myContact.FullName := ContactsFullName.Strings[1];      . . .    end; I have never coded COMOBJ before that, so I would really appreciate any assistance. ---------------------------------------------------------------------- taazz · 2014-09-20 10:04 try typecasting the sting to widestring and see where that gets you. Please explain when you say "it does not work" what exactly do you mean? As far as I can see you must have at least an empty contact in outlook. ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-20 10:32 Quote from: drschwarcz on September 20, 2014, 11:37:51 amI want to create a new contact to Outlook. The code is working when I type the strint to the code. Like that: myContact.FullName := 'Full name'; But is not worknig when I want give from a String type (TStringList). myContact.FullName := ContactsFullName.Strings[1]; @Taazz is correct, you simply haven't given us enough information.  What is the error you're getting?   And is it at compile time, or run time? However, looking at the code above, the most likely error you're getting is Index out of bounds.  This has nothing to do with COM programming at all.   TStringLists start their index at 0, not 1.  I'm guessing you simply need to change your code to Code: [Select]myContact.FullName := ContactsFullName.Strings[0]; Another possibility is you're getting is an AV of sorts.  There is always the chance you haven't created ContactsFullName yet, and are calling .Strings[1] on a nil object.  If the above doesn't help, please let us know your exact error. ---------------------------------------------------------------------- drschwarcz · 2014-09-20 18:27 THX, guys. You are right, I have not given enough info. The code is running "correctly" (I do not get any error message), just the contact is not created. Related to TStringList, I do not have issue with it. I created and filled it in another part of program (It has 11 item currently). As Taazz wrote, the variant (myContact.FullName) is needed WideString, but TStringList contains simply (ansi)String. Unfortunately, I could not find any "usable" solution for conversion. ps: I'm a quite amateur for programming. I learnt Turbo Pascal for many years ago and self-taught a bit Delphi. Thus,  I'm very grateful for all the help and patience. ---------------------------------------------------------------------- taazz · 2014-09-20 18:39 a simple solution would be to do something like this. Code: [Select]procedure AddAddressInDistributionList; const   olFolderContacts = $0000000A; var   myOlApp, myNameSpace, myContact : OleVariant;   vStr : widestring; begin   //Connect to outlook   myOlApp := CreateOleObject('Outlook.Application');   vStr := 'MAPI';   myNameSpace:=myOlApp.GetNameSpace(vStr);   //Get root contacts folder   ContactsFolder := myNameSpace.GetDefaultFolder(olFolderContacts);   //Create the new Contact   myContact:=myOlApp.CreateItem(ContactsFolder.Folders.Count);   vStr := ContactsFullName.Strings[1];   myContact.FullName := vStr;   .   .   . end; This will instruct the compiler to add code to convert the ansistring to a widestring automagicaly and leave at that for now, also make sure that the name space is correct that in that name space you the permisions required to create a contact if there is such thing as permitions on the mapi space I'm out of depth from here on out I never really done any office interoperability so I can test and see what might be wrong in the delphi side of things but that is all for now. One last thing there is a beautiful library in sourceforge called TOfficePartner or something along those  lines by turbo power a company that got out of the delphi component business a while back since it is a library about  managing office applications is not mentioned a lot in this forums which are multi platform focused but since you are trying to work with office I guess you will not mind a windows only library. ---------------------------------------------------------------------- Mike.Cornflake · 2014-09-20 19:22 If after @Taazz's suggestion, your outlook contact still isn't created, can you please post the entire procedure AddAddressInDistributionList? ---------------------------------------------------------------------- drschwarcz · 2014-09-21 08:04 THX Taazz. It was too easy to me.  I wanted to convert in any case!  I feel myself getting stupider. Thank you very much Guys.