• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 677
  • Last Modified:

Outlook and Delphi

Hi All,

I need to know how I can get the "All mail folders"/ "personal folders" from my (Microsoft Outlook 2003) and display it in a TListBox component on a form using Delphi 7

So can someone provide me with source code OR tutorial (with source) that shows how I can do that.

Thanks in advance
0
GiantMatrix
Asked:
GiantMatrix
  • 2
1 Solution
 
FarajLYCommented:
Greeting GiantMatrix,

Try this code:

********  File name.pas*************

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    ListBox2: TListBox;
    Memo1: TMemo;
    ListBox3: TListBox;
    procedure Button1Click(Sender: TObject);
    procedure ListBox1DblClick(Sender: TObject);
    procedure ListBox2DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  const  olMailItem = 0;
var
  Outlook: OleVariant;
  vMailItem,myFolder: variant;
  i,j:integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 ListBox1.Clear;  // your personal folders
 ListBox2.Clear; // your Subject
 ListBox3.Clear; // your Index personal folders
 memo1.Clear; // your email body

 try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;

  vMailItem := Outlook.GetNamespace('MAPI') ;
 for i:=3 to 6 do
 begin
  j:=0;
  try
    myFolder := vMailItem.GetDefaultFolder(i) ;
   except
    j:=1;
 end;
  if j<>1 Then
     begin
        ListBox1.Items.Add(myFolder);
        ListBox3.Items.Add(inttostr(i));
     end;
     j:=myFolder.items.count ;
end;

 //for i:=1 to j do
   //  ListBox2.Items.Add(myFolder.items(i));

   //  Memo1.clear;
    // Memo1.Lines.Add(myFolder.items(15).body);
end;
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
  myFolder := vMailItem.GetDefaultFolder(ListBox3.Items.Strings[ListBox1.ItemIndex]) ;
  ListBox2.Clear;
  j:=myFolder.items.count ;
  for i:=1 to j do
     ListBox2.Items.Add(myFolder.items(i));

end;

procedure TForm1.ListBox2DblClick(Sender: TObject);
begin
      Memo1.clear;
      Memo1.Lines.Add(myFolder.items(ListBox2.ItemIndex+1).body);
end;

end.

******** EOF.pas************

*************Form********************
object Form1: TForm1
  Left = 90
  Top = 107
  Width = 922
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 8
    Top = 8
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object ListBox1: TListBox
    Left = 96
    Top = 8
    Width = 257
    Height = 377
    ItemHeight = 13
    TabOrder = 1
    OnDblClick = ListBox1DblClick
  end
  object ListBox2: TListBox
    Left = 360
    Top = 8
    Width = 257
    Height = 377
    ItemHeight = 13
    TabOrder = 2
    OnDblClick = ListBox2DblClick
  end
  object Memo1: TMemo
    Left = 624
    Top = 16
    Width = 241
    Height = 369
    Lines.Strings = (
      'Memo1')
    TabOrder = 3
  end
  object ListBox3: TListBox
    Left = 8
    Top = 64
    Width = 49
    Height = 145
    ItemHeight = 13
    TabOrder = 4
  end
end


0
 
GiantMatrixAuthor Commented:
FarajLY,

Perfect :-)  Just 3 final question about the source code you posted:

- Why the (Junk E-mail) and (Drafts) folders are not shown when retrieving the folders from outlook?
- Is there a way I can disable showing the warning message when double clicking on ListBox2 to see the messages content?
- Why do I get an error at line (Outlook := GetActiveOleObject('Outlook.Application');) when Outlook is not running  and is there a way I can avoid this error?

thanks in advance for your help and cooperation.
0
 
FarajLYCommented:
Greeting GiantMatrix
- about (Junk E-mail) and (Drafts) folders there have code (23) ,(16) you can try it ,(for i:=3 to 6 ) to (for i:=3 to 23 ) and disable this line  j:=myFolder.items.count ; to  // j:=myFolder.items.count  in Button1Click(Sender)
- can disable showing the warning message when double clicking on ListBox2,only i know make click in allow access for
-  Why do I get an error at line (Outlook := GetActiveOleObject('Outlook.Application');) when Outlook is not running  and is there a way I can avoid this error?
because you did run from delphi , but when do compiler or (run) and close this application and go to it's folder and open exe file

sorry my English language not good
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now