Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: 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.

In reply to Adding Outlook Contacts In Perl by jgamble

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (11)
    As of 2018-07-20 13:06 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (431 votes). Check out past polls.