Recent

Author Topic: [Solved]How can I add a string to Variant type?  (Read 9180 times)

drschwarcz

  • New Member
  • *
  • Posts: 17
[Solved]How can I add a string to Variant type?
« on: September 20, 2014, 11:37:51 am »
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.

 :)
« Last Edit: September 21, 2014, 10:05:23 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 :) ;)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How can I add a string to Variant type?
« Reply #1 on: September 20, 2014, 12:04:59 pm »
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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: How can I add a string to Variant type?
« Reply #2 on: September 20, 2014, 12:32:18 pm »
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];

@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.
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: How can I add a string to Variant type?
« Reply #3 on: September 20, 2014, 08:27:42 pm »
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.  :)
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 :) ;)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How can I add a string to Variant type?
« Reply #4 on: September 20, 2014, 08:39:25 pm »
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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: How can I add a string to Variant type?
« Reply #5 on: September 20, 2014, 09:22:23 pm »
If after @Taazz's suggestion, your outlook contact still isn't created, can you please post the entire procedure AddAddressInDistributionList?
« Last Edit: September 20, 2014, 10:26:46 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: How can I add a string to Variant type?
« Reply #6 on: September 21, 2014, 10:04:42 am »
THX Taazz. It was too easy to me.  :D I wanted to convert in any case!  :( I feel myself getting stupider.

Thank you very much Guys.  :)
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