2008. december 5., péntek

Outlook Automation - Contactlist


Problem/Question/Abstract:

How use Outlook's Contact list in Applications

Answer:

This is sample how look and change information in Outlook's Contactlist from external application.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
const
  // constants from MSOUTL8.olb

  olByValue = 1;
  olByReference = 4;
  olEmbeddedItem = 5;
  olOLE = 6;

  olMailItem = 0;
  olAppointmentItem = 1;
  olContactItem = 2;
  olTaskItem = 3;
  olJournalItem = 4;
  olNoteItem = 5;
  olPostItem = 6;

  olFolderDeletedItems = 3;
  olFolderOutbox = 4;
  olFolderSentMail = 5;
  olFolderInbox = 6;
  olFolderCalendar = 9;
  olFolderContacts = 10;
  olFolderJournal = 11;
  olFolderNotes = 12;
  olFolderTasks = 13;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    mName: TMemo;
    mFamily: TMemo;
    mFullName: TMemo;
    mCompany: TMemo;
    mSaveAs: TMemo;
    mBody: TMemo;
    btnSave: TButton;
    sbContacts: TScrollBar;
    btnConnect: TButton;
    procedure btnConnectClick(Sender: TObject);
    procedure mNameChange(Sender: TObject);
    procedure sbContactsChange(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
  private
    { Private declarations }
    IsDirty: boolean;
  public
    { Public declarations }
    OlApp, Namespace, ContactFolder: OleVariant;
    procedure LoadContact(I: Integer; folder: OleVariant);
    procedure SaveContact(I: Integer; folder: OleVariant);
  end;

var
  Form1: TForm1;

implementation
uses ComObj;
{$R *.DFM}

procedure TForm1.btnConnectClick(Sender: TObject);
begin
  OlApp := CreateOleObject('Outlook.Application');
  Namespace := OlApp.GetNameSpace('MAPI');
  ContactFolder := Namespace.GetDefaultFolder(olFolderContacts);
  sbContacts.Max := ContactFolder.Items.Count;
  sbContacts.Position := 1;
  LoadContact(1, ContactFolder);
end;

procedure TForm1.LoadContact(I: Integer; folder: OleVariant);
var
  Item: OleVariant;
begin
  Caption := 'Rec ' + IntToStr(i) + ' from ' + IntToStr(sbContacts.Max);
  Item := folder.Items(I);
  mName.Text := Item.FirstName;
  mFamily.Text := Item.LastName;
  mFullName.Text := Item.FullName;
  mCompany.Text := Item.CompanyName;
  mSaveAs.Text := Item.FileAs;
  mBody.Text := Item.Body;
end;

procedure TForm1.SaveContact(I: Integer; folder: OleVariant);
var
  Item: OleVariant;
begin
  Item := folder.Items(I);
  Item.FirstName := mName.Text;
  Item.LastName := mFamily.Text;
  Item.FullName := mFullName.Text;
  Item.CompanyName := mCompany.Text;
  Item.FileAs := mSaveAs.Text;
  Item.Body := mBody.Text;
  Item.Save;
  isDirty := False;
end;

procedure TForm1.mNameChange(Sender: TObject);
begin
  IsDirty := True;
end;

procedure TForm1.sbContactsChange(Sender: TObject);
begin
  LoadContact(sbContacts.Position, ContactFolder);
end;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
  SaveContact(sbContacts.Position, ContactFolder);
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése