Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Extract Outlook Telephone Numbers

by Corion (Patriarch)
on Mar 02, 2001 at 19:10 UTC ( #61803=sourcecode: print w/replies, xml ) Need Help??
Category: Win32 Stuff; OLE Automation
Author/Contact Info Max Maischein (Corion)
Description: This is a small example on how to use the MS Outlook object model from within Perl (under Win32 of course) to extract contacts data. If you have the Outlook object model chart at hand, it's easy to automate other tasks such as sending mail or creating contacs as well. This snippet needs Outlook to be open and the Contacts view must be active, because I was too lazy to drill my way down from Outlook->Application to the Contacts view.

Update:I added the missing <code> tags myself. Even more stupid I am ...

Update 2:Some documentation - the 069 is my local area code.

#!/usr/bin/perl -w

use strict;
use Win32::OLE;

my @PhoneProperties = (

my $outlook;
$outlook = Win32::OLE->new('Outlook.Application');

my $activeexplorer;
$activeexplorer = $outlook->ActiveExplorer;

print $activeexplorer->Caption,"\n";

my $items = $activeexplorer->CurrentFolder->Items;
print $items->Count,"\n";

my $linenumber = 1;
print "NR;NAME;TELNUM\n";

my $Kontakt;
my $PhoneProp;
my $ItemIndex = 1;

while ($ItemIndex <= $items->Count) {
  $Kontakt = $items->item($ItemIndex);

  foreach $PhoneProp (@PhoneProperties) {
    my $number = $Kontakt->{"$PhoneProp"};
    if ($number) {
      # Prefix local area code unless an area code
      # is already given
      if ($number !~ /^0/) { $number = "069$number" };
      $number =~ s/ +//g;
      $number =~ s/-+//g;
      print $linenumber++, ";", $Kontakt->FullName,";'$number\n";
      #print "$number=", substr( $Kontakt->FullName, 1, 15 ),"\n";

Replies are listed 'Best First'.
Re: Extract Outlook Telephone Numbers
by dze27 (Pilgrim) on Mar 03, 2001 at 01:49 UTC

    neat! This worked for me except it did not print the Caption on line 26. I'm using Outlook 97 (poor me) so maybe that's the problem. $activeexplorer->CurrentFolder->Name seems to work though.

    What is the meaning of 069 (line 46)?

    I added some code after line 24 to automatically change to the Contacts folder:

    use Win32::OLE::Const 'Microsoft Outlook';
    my $outlook; $outlook = Win32::OLE->new('Outlook.Application'); my $namespace = $outlook->GetNamespace("MAPI"); my $Folder = $namespace->GetDefaultFolder(olFolderContacts); print $Folder->Name,"\n"; my $items = $Folder->Items; print $items->Count,"\n";
    does it. (Between "my $outlook;" and "print $items->Count,"\n";)

    This doesn't change folders on the running copy of Outlook. The extra "use" line is required for using olFolderContacts.

Re: Extract Outlook Telephone Numbers
by jlf (Scribe) on Mar 08, 2002 at 22:29 UTC

    In the spirit of TMTOWTDI, check this node for a way to look up phone numbers without having an Outlook session open.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://61803]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2022-08-16 06:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found