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.