oedocs GS, NY


home > Outlook & Exchange - programming Email

Outlook & Exchange - programming

intro
codes1


intro home - top of the page - email

- http://www.cdolive.com/code.htm - CDO Live - Code Sample Library ( http://www.cdolive.com/mbxreport.htm )
- http://msdn.microsoft.com/library/default.asp?URL=/library/techart/outlookanchor.htm - Outlook Technical articles
- http://search.support.microsoft.com/kb/c.asp - Microsoft Knowledge Base
- http://support.microsoft.com/support - Microsoft Support Web site
- http://www.microsoft.com/office/developer - Microsoft Office Developer Support Web site
- http://msdn.microsoft.com - Microsoft Developer Network
- http://mspress.microsoft.com - Microsoft Press
- http://support.microsoft.com/support/news - Public Internet newsgroups
- http://exchange.devx.com/ - Exchange & Outlook magazine


codes1 home - top of the page - email

Getting information from Contacts folder localy:
use strict; 
use Win32::OLE; 
use Win32::OLE::Const 'Microsoft Outlook'; 
# or 'Microsoft Outlook 9.0 Object Library';
# Const module defines many usefule constants - like olFolderContacts , etc.

my $Outlook = Win32::OLE->GetActiveObject(' Outlook.Application') or die "oops 1"; 
my $namespace = $Outlook->GetNamespace(" MAPI "); 
my $Folder = $namespace->GetDefaultFolder( olFolderContacts) or die "oops 2"; 

#  my $NewFolder = $Folder->Folders->Add("test_folder");

my $contacts = $Folder->{Items}; 
my $ncon = $contacts->{Count}; 
  print "number of contacts - $ncon\n\n"; # this works 

#  print '-'x50,"\n"; 
#  map {print "$_\n" } keys %{$contacts->Item(1)}; 
#  print '-'x50,"\n"; 

my $con 
for my $ii (1 .. $ncon) { 
  $con = $contacts->Item($ii); 
  print "$ii - $con->{FullName}\n"; 

print "\nAnother way to iterate\n\n";

$con=$contacts->GetFirst();    print "$con->{FullName}\n"; 
while ($con=$contacts->GetNext()) {  print "$con->{FullName}\n";  }

List Address Lists available in your Outlook:
use strict; 
use Win32::OLE; 
use Win32::OLE::Const 'Microsoft Outlook'; 

my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application') 
  or die "oops GetActiveObject\n"; 
   # $Outlook = Win32::OLE->new('Outlook.Application', 'Quit');

my $namespace = $Outlook->GetNamespace("MAPI"); 
my $addrlists = $namespace->{AddressLists} 
  or die "oops AddressLists\n"; 
my $alcount = $addrlists->{Count}; 
for my $ii (1..$alcount) { 
  my $Item = $addrlists->Item($ii); 
  my $name = $Item->{Name}; 

  print "$ii = $name\n"; 
}

print information about members of a mailing list:
use strict; 
use Win32::OLE; 
use Win32::OLE::Const 'Microsoft Outlook'; 

my $Outlook = Win32::OLE->GetActiveObject(' Outlook.Application') 
  or die "oops GetActiveObject\n"; 
my $namespace = $Outlook->GetNamespace("MAPI "); 
my $gal = $namespace->AddressLists("Global Address List"); 
my $addr = $gal->AddressEntries("some-list "); 
my $members = $addr->{Members}; 
for my $ii (1 .. $members->{Count}) { 
  my $rh = $members->Item($ii); 
  my $name_ = $rh->{Name}; 
  my $id_   = $rh->{ID}; 
  print "$name_\n  $id_\n"; 
}

Use CDO to connect to Exchange
and get the number of contacts
from other people's folders:
use strict; 
use Win32::OLE; 
use Win32::OLE::Variant; 
use Win32::OLE::Const; 

my $DEBUG_FLAG = 1; # set it to zero for real job 
my $mailing_list_name = "some-list";  # set it to real name 
my $server = "abcde01";
my $fname = "john";
my $lname = "smith";
 

# Necessary for MAPI component 
Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE); 

# Do this rather than 'use' as that interferes with the OLE Initialize code 
my $CDO_CONST = Win32::OLE::Const->Load('Microsoft CDO.*Library'); 

my $MAPI = Win32::OLE->new('MAPI.Session'); 
die "Could instantiate MAPI Session: " . Win32::OLE::LastError() if (! defined($MAPI)); 

my %LogonParms = ( 'ProfileInfo' => "$server\n$fname.$lname" ); 

$MAPI->logon(\%LogonParms); 
die Win32::OLE::LastError() if (Win32::OLE::LastError()); 
print $MAPI->{CurrentUser}->Value . "\n"; 
my $GAL = $MAPI->AddressLists("Global Address List")->{AddressEntries}; 

my $Filter = $GAL->Filter(); 

$Filter->{Name} = $mailing_list_name;  # "gs-emaildev"; 

my $DL = $GAL->GetFirst(); # Assumes unique match to above filter 
my $Members = $DL->{Members}; 

for ( my $Member = $Members->GetFirst(); 
      $Member != undef; 
      $Member = $Members->GetNext() ) { 
  my $name = $Member->{DisplayName}; 
  print "Processing $name\n"; 
  if ($DEBUG_FLAG && $name !~ /$lname/i) { 
    print "debug - no access\n" 
  } else { 
    ProcessMbx($Member->Fields($$CDO_CONST{CdoPR_ACCOUNT})->Value); 
  } 

exit; 
# ------------------------------------------------------------ 
# end of the main part 
# ------------------------------------------------------------ 
 

# ------------------------------------------------------------ 
# ------------------------------------------------------------ 
sub ProcessMbx { 
    my $Account = shift; 

    my $MAPI = Win32::OLE->new('MAPI.Session'); 
    die "Could instantiate MAPI Session: " . Win32::OLE::LastError() 
      if (! defined($MAPI)); 

    my %LogonParms = ( 'ProfileInfo' => "gsnb01e\n$Account" ); 
    $MAPI->logon(\%LogonParms); 
    die Win32::OLE::LastError() if (Win32::OLE::LastError()); 

    my $Contacts = $MAPI->GetFolder( 
      $MAPI->{Inbox}->{Fields}->Item(0x36D10102), 
      $MAPI->{Inbox}->{StoreID} ); 
    my $Items = $Contacts->{'Messages'}; 
    print "\tContacts - " . $Items->{'Count'} . "\n"; 
}


 
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Outlook';
use Win32::OLE::Variant;

my $Outlook = Win32::OLE->new('Outlook.Application', 'Quit');
my $ol = Win32::OLE::Const->Load($Outlook);
my $namespace = $Outlook->GetNamespace("MAPI");
my $Folder = $namespace->GetDefaultFolder(olFolderContacts);

#  my $NewFolder = $Folder->Folders->Add("test_folder");

my $contacts = $Folder->{Items};
my $ncon = $contacts->{Count};
print "number of contacts - $ncon\n\n"; # this works

my $con;
for my $ii (1 .. $ncon) {
  $con = $contacts->Item($ii);
  print "$ii - $con->{FullName}\n";
  print "Phone is $con->{HomeTelephoneNumber}\n";
  my $x = $$con{LastModificationTime};
  print "==2==  ", $x,"\n",
        "--2-- " , $x->Date('dddd MMM dd, yyyy') , ' '
                 , $x->Time('hh:mm:ss tt') , "\n";
}

__END__
Z:\AAA_work\myperl>test.pl
number of contacts - 2
 

1 - TestContact1
Phone is
==2==  12/18/01 4:25:05 PM
--2-- Tuesday Dec 18, 2001 04:25:05 PM
2 - TestContact2
Phone is
==2==  10/1/01 12:04:40 PM
--2-- Monday Oct 01, 2001 12:04:40 PM