Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise ones, I am searching for a map into the import function in Outlook for contacts. I have been somewhat successful with doing simple emails via Outlook (Via Unix - but I am now in a M$ shop). I have seen several sites that have mapped out a lot with excel and word using win32::OLE but not much on Outlook. All I really want to accomplish is to take my csv file produced from my perl/mysql/cgi entry system and load it into my contacts and of course I want to do this automagikally :>) Thanks to all.
  • Comment on Perl control of Outlook import to contacts with win32::OLE

Replies are listed 'Best First'.
Re: Perl control of Outlook import to contacts with win32::OLE
by cacharbe (Curate) on May 23, 2002 at 19:52 UTC
    Well, working your way through the wizard is a PIA, especially since you can add them yourself directly through the OLE interface.
    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Outlook'; $|++; $Win32::OLE::Warn = 3; # Die on errors my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application', 'Quit'); my $NameSpace = $OL->GetNameSpace("MAPI"); #You can also use $NameSpace->GetDefaultFolder(olFolderContacts) if yo +u #just want to put them in the default. my $Contacts = $NameSpace->Folders("Personal Folders")->Folders(" +Contacts"); my $NewContact = $Contacts->Items->Add(); $NewContact->{FullName}="Your Name Here"; $NewContact->{PagerNumber}="xxx.xxx.xxxx"; $NewContact->Save(); #And there are about 175 other properties that you can set.
    If you have AS Perl installed, it comes with a nice little object browser that you can use to investigate all those wonderful properties a contact item can have. You are interested in looking at the ContactItem class of the Application Object

    The browser was conveniently located at:

    c:\perl\html\site\lib\win32\ole\browser\browser.html

    Hope that helps a bit.

    C-.

      Ok C - my testing goes well until I installed this subroutine in my cgi script:
      sub OutLookAdd{ my ($id) = @_; my $sql = qq!select phones.id, phones.first_name, phones.last_name, phones.identifier, phones.prefix1, phones.suffix1, phones.prefix2, phones.suffix2, phones.site, phones.location, phone_location.title from phones, phone_location where phones.id = $id and phones.phone_location_id = phone_location.id!; $db_action = $DB->prepare($sql); $db_action->execute or die "Could not do $sql - $DBI::errstr"; my $rec = $db_action->fetchrow_hashref; $db_action->finish; my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application', 'Quit'); my $NameSpace = $OL->GetNameSpace("MAPI"); my $Contacts = $NameSpace->Folders("Mailbox - Norris, Joseph")->Folders("test_co +ntacts"); $rec->{title} =~ s/\\//g; my $NewContact = $Contacts->Items->Add(); $NewContact->{FullName}= $rec->{first_name} . ' ' . $rec->{las +t_name}; $NewContact->{Title} = $rec->{title}; $NewContact->{FirstName} = $rec->{first_name}; $NewContact->{MiddleName} = '1'; $NewContact->{LastName} = $rec->{last_name}; $NewContact->{Suffix} = '2'; $NewContact->{Company} = $rec->{title}; $NewContact->{Department} = '3'; $NewContact->{JobTitle} = '4'; $NewContact->{BusinessStreet} = '5'; $NewContact->{BusinessStreet2} = '6'; $NewContact->{BusinessStreet3} = '7'; $NewContact->{BusinessCity} = '8'; $NewContact->{BusinessState} = '9'; $NewContact->{BusinessPostalCode} = '10'; $NewContact->{BusinessCountry} = '11'; $NewContact->{HomeStreet} = '12'; $NewContact->{HomeStreet2} = '13'; $NewContact->{HomeStreet3} = '14'; $NewContact->{HomeCity} = '15'; $NewContact->{HomeState} = '16'; $NewContact->{HomePostalCode} = '17'; $NewContact->{HomeCountry} = '18'; $NewContact->{OtherStreet} = '19'; $NewContact->{OtherStreet2} = '20'; $NewContact->{OtherStreet3} = '21'; $NewContact->{OtherCity} = '22'; $NewContact->{OtherState} = '23'; $NewContact->{OtherPostalCode} = '24'; $NewContact->{OtherCountry} = '25'; $NewContact->{BusinessFax} = '26'; $NewContact->{BusinessPhone} = join '-', $rec->{prefix1}, $rec->{suffix1}; if ($rec->{prefix2}){ $NewContact->{BusinessPhone2} = join '-', $rec->{prefix2}, $rec->{suffix2}; } else { $NewContact->{BusinessPhone2} = '27'; } $NewContact->{Callback} = '28'; $NewContact->{CarPhone} = '29'; $NewContact->{CompanyMainPhone} = '30'; $NewContact->{HomeFax} = '31'; $NewContact->{HomePhone} = '32'; $NewContact->{HomePhone2} = '33'; $NewContact->{ISDN} = '34'; $NewContact->{MobilePhone} = '35'; $NewContact->{OtherFax} = '36'; $NewContact->{OtherPhone} = '37'; $NewContact->{Pager} = '38'; $NewContact->{PrimaryPhone} = join '-', $rec->{prefix1}, $rec->{suffix1}; $NewContact->{RadioPhone} = '39'; $NewContact->{Telex} = '40'; $NewContact->{Account} = '41'; $NewContact->{Anniversary} = '42'; $NewContact->{BillingInformation} = '43'; $NewContact->{Birthday} = '44'; $NewContact->{Categories} = '45'; $NewContact->{Children} = '46'; $NewContact->{DirectoryServer} = '47'; $NewContact->{"E-mail Address"} = 'mytest@mytest.com'; $NewContact->{"E-mail Type"} = '48'; $NewContact->{"E-mail Display Name"} = '49'; $NewContact->{"E-mail2 Address"} ='50'; $NewContact->{"E-mail2 Type"} = '51'; $NewContact->{"E-mail2 Display Name"} = '52'; $NewContact->{"E-mail3 Address"} = '53'; $NewContact->{"E-mail3 Type"} = '54'; $NewContact->{"E-mail3 Display Name"} = '55'; $NewContact->{Gender} = '56'; $NewContact->{GovernmentIDNumber} = '57'; $NewContact->{Hobby} = '58'; $NewContact->{Location} = $rec->{location}; $NewContact->{User1} = $rec->{site}; $NewContact->Save(); }
      When I run the cgi I get an update to my database and then a return of information but when the ole stuff gets invoked I get:
      Win32::OLE(0.1502) error 0x80080005: "Server execution failed" at c:\w +eb\cgi-bin\PHONEL~1\new.pl line 209 eval {...} called at c:\web\cgi-bin\PHONEL~1\new.pl line 209 main::OutLookAdd(407) called at c:\web\cgi-bin\PHONEL~1\new.pl lin +e 183 main::additem2 called at c:\web\cgi-bin\PHONEL~1\new.pl line 84
      Any idea what I am doing wrong? Thanks.
        I can tell you right off the top that a bunch of your property names are incorrect. Here is what the property names should be (I've only included the ones to which you were actually giving data):
        $NewContact->{FullName}= $rec->{first_name} . ' ' . $rec->{last_name}; $NewContact->{Title} = $rec->{title}; $NewContact->{FirstName} = $rec->{first_name}; $NewContact->{LastName} = $rec->{last_name}; $NewContact->{BusinessTelephoneNumber} = join '-', $rec->{prefix1}, $rec->{suffix1}; if ($rec->{prefix2}){ $NewContact->{Business2TelephoneNumber} = join '-', $rec->{prefix2}, $rec->{suffix2}; } else { $NewContact->{Business2TelephoneNumber} = '27'; } $NewContact->{CompanyName} = $rec->{company}; $NewContact->{PrimaryTelephoneNumber} = join '-', $rec->{prefix1}, $rec->{suffix1}; $NewContact->{OfficeLocation} = $rec->{location}; $NewContact->{User1} = $rec->{site}; $NewContact->{"Email1Address"} = 'mytest@mytest.com';
        See if that helps. Use that browser that I pointed out to actually investigate the 'ContactItem' class object, and use the exact name given for the other properties in question.

        C-.

        Update: To answer your other questions. You should read the Win32::OLE documentation first. The GetActiveObject method is described there, as it's a part of that package.

      This was very helpful - especially about the browser. I tried your code - changing to look for my contact file of course - and I got the following: OLE exception from "Microsoft Outlook": The operation failed. An object could not be found. Win32::OLE(0.1502) error 0x8004010f in METHOD/PROPERTYGET "Folders" at contact.pl line 13 Any ideas? A question on the browser - To find GetActiveObject where would I go. Thanks again.
        I made it work - I had the wrong file folder name. Now I need to know how to update and delete. Boy this is 10 times better than what I had thought of. Thanks a bunch. I still need to know how to find those items I mentioned above in the browser.