Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Adding Outlook Contacts In Perl

by jgamble (Pilgrim)
on Feb 07, 2007 at 19:42 UTC ( #598865=perlmeditation: print w/ replies, xml ) Need Help??

I had a emergency call to transfer address book contents from a client's old system to Microsoft Outlook. At first this seemed simple. The old system exported its contents in a well-formed VCF file, each vCard separated by a double newline. But Outlook assumes that each VCF file holds only one vCard, and imports only the first entry, skipping the rest.

That's simple (I thought). Set $/ to "\n\n", slurp in the file to an array variable, and write out each record as an individual file in a subdirectory for each user.

Oops. Not so simple. Outlook doesn't do batch reads of VCF files. Someone would have to click on over a thousand VCF files to enter them into Outlook, and since that someone would be me it was vitally important to come up with a different solution.

A search through Google showed this to be a problem that almost no one had bothered to solve. But I had written an Outlook e-mail subroutine once, and thought it could be modified to handle contact items instead of mail items.

Here's the outer loop around the new subroutine. At this point the records have been slurped into the array @vrecs. I ignored groups, which were done in an idiosyncratic way in the old system. Some vcard items cross multiple lines, so I temporarily join them. Then I split the entries by newlines, and created a hash to feed to outlook_vcard().

# # Create a contact for each record. # foreach my $vcf (@vrecs) { my %track; unless ($vcf =~ /^X-GWTYPE:GROUP(END)?/) # Skip group records. { chomp $vcf; # $/ is still "\n\n". $vcf =~ s/=0A=\n/=0A= /g; my @lines = split(/\n/, $vcf); foreach my $line (@lines) { my @parts = split(/:/, $line); $parts[1] =~ s/=0A= /\n/g; $track{$parts[0]} = $parts[1]; } } outlook_vcard(%track); }

Since the outlook_vcard function was written to handle a specific problem, not all possible VFC entries are dealt with. For example, a quick search of the client's files showed that only one e-mail address per entry was present, so I didn't have to worry about dealing with multiple e-mail addresses, something that wouldn't be true in a more generic application.

sub outlook_vcard { my(%vcard_props) = @_; my $mail = new Win32::OLE('Outlook.Application'); my $item = $mail->CreateItem(2); # 2 = contact item. unless ($item) { warn "Outlook is not running, cannot save vCard.\n"; return 0; } $item->{FullName} = $vcard_props{'FN'} || $vcard_props{'N'} || ' '; $item->{Company} = $vcard_props{'ORG'} || ' '; $item->{Email1Address} = $vcard_props{'EMAIL;WORK;PREF'} || $vcard_props{'EMAIL;WORK;PREF;NGW'} || ' '; $item->{HomeAddress} = $vcard_props{'ADR;DOM;HOME;PARCEL;POSTAL'} | +| $vcard_props{'LABEL;DOM;HOME;PARCEL;POSTAL;ENCODING=QUOTED-PR +INTABLE'} || ' '; $item->{BusinessAddress} = $vcard_props{'ADR;DOM;WORK;PARCEL;POSTAL +'} || $vcard_props{'LABEL;DOM;WORK;PARCEL;POSTAL;ENCODING=QUOTED-PR +INTABLE'} || ' '; $item->{OtherAddress} = $vcard_props{'ADR;INTL;WORK;PARCEL;POSTAL'} + || $vcard_props{'LABEL;INTL;WORK;PARCEL;POSTAL;ENCODING=QUOTED-P +RINTABLE'} || ' '; $item->{BusinessTelephoneNumber} = $vcard_props{'TEL;WORK'} || ' '; $item->{MobileTelephoneNumber} = $vcard_props{'TEL;CELL'} || ' '; $item->{HomeTelephoneNumber} = $vcard_props{'TEL;HOME'} || ' '; $item->{PrimaryTelephoneNumber} = $vcard_props{'TEL;PREF'} || ' '; $item->{BusinessFaxNumber} = $vcard_props{'TEL;PREF;FAX'} || ' '; $item->{Title} = $vcard_props{'TITLE'} || ' '; $item->{Body} = $vcard_props{'NOTE'} || $vcard_props{'NOTE;QUOTED-PRINTABLE'} || ' '; $item->Save(); return 1; }

  1. Create a contact in Outlook using VB.
  2. Another article on the same subject.
  3. The properties of ContactItem.

Comment on Adding Outlook Contacts In Perl
Select or Download Code
Re: Adding Outlook Contacts In Perl
by halley (Prior) on Feb 07, 2007 at 19:52 UTC
    Are you sure Outlook does not support batch imports of .vcf files? In the version I have installed, I was able to drop multiple files onto the contacts view, and it imported all of them correctly. There may be extenuating issues with various versions, but try more than one way to import before doing a lot of work.

    --
    [ e d @ h a l l e y . c c ]

      (Hmph. I could have sworn that I replied to this a couple of hours ago.)

      Well, in the version of Outlook that I was using, it did not allow me to drag a cluster of vcf files to the Contacts pane, and the open dialog didn't support multiple choices.

      I take it from your comment that you used the drag-and-drop method?

Re: Adding Outlook Contacts In Perl
by jgamble (Pilgrim) on Feb 07, 2007 at 23:19 UTC

    Incidently, should anyone want to create other types of items in Outlook, the values CreateItem understands are:

    my %outlook_items = ( olMailItem => 0, olAppointmentItem => 1, olContactItem => 2, olTaskItem => 3, olJournalItem => 4, olNoteItem => 5, olPostItem => 6, olDistributionListItem => 7, );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://598865]
Front-paged by kyle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-12-26 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls