Delphi中读取Outlook的数据,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, activex, ComObj, ComCtrls; type TForm1 = class(TForm) btnGetData: TButton; lvCalendar: TListView; PageControl1: TPageControl; TabSheet1: TTabSheet; TabSheet2: TTabSheet; lvContact: TListView; procedure btnGetDataClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btnGetDataClick(Sender: TObject); var i, J: Integer; Outlook, Namespace, Appointment, Calendar: variant; Created: Boolean; Accept: Boolean; MyItems: Variant; apptItem, Contacts, cItem: Variant; const olAppointmentItem = 1; olFolderCalendar = 9;// The Calendar folder. olFolderConflicts =19;// The Conflicts folder (subfolder of Sync Issues folder). Only available for an Exchange account. olFolderContacts =10;// The Contacts folder. olFolderDeletedItems = 3;// The Deleted Items folder. olFolderDrafts = 16;// The Drafts folder. olFolderInbox =6;// The Inbox folder. olFolderJournal= 11;// The Journal folder. olFolderJunk = 23;// The Junk E-Mail folder. olFolderLocalFailures =21;// The Local Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account. olFolderManagedEmail = 29;// The top-level folder in the Managed Folders group. For more information on Managed Folders, see Help in Microsoft Outlook. Only available for an Exchange account. olFolderNotes= 12 ;// The Notes folder. olFolderOutbox = 4;// The Outbox folder. olFolderSentMail = 5;// The Sent Mail folder. olFolderServerFailures = 22;// The Server Failures folder (subfolder of Sync Issues folder). Only available for an Exchange account. olFolderSyncIssues = 20;// The Sync Issues folder. Only available for an Exchange account. olFolderTasks= 13;// The Tasks folder. olFolderToDo =28;// The To Do folder. olPublicFoldersAllPublicFolders= 18;// The All Public Folders folder in the Exchange Public Folders store. Only available for an Exchange account. olFolderRssFeeds = 25;// The RSS Feeds folder. begin lvCalendar.Clear ; Created := False; try Outlook := CreateOleObject('Outlook.Application'); except Created := True; end; Namespace := Outlook.GetNamespace('MAPI'); Calendar := Namespace.GetDefaultFolder(olFolderCalendar); for I := 1 to Calendar.Items.count do begin Appointment := Calendar.Items[i]; try With lvCalendar.Items.Add do begin Caption := Appointment.Subject; SubItems.Add(Appointment.Start); SubItems.Add(Appointment.End); SubItems.Add(Appointment.Duration); SubItems.Add(Appointment.Location); SubItems.Add(Appointment.Body); end; except end; end; Contacts := Namespace.GetDefaultFolder(olFolderContacts); for I := 1 to Contacts.Items.Count do begin Citem := Contacts.Items[I]; try With lvContact.Items.Add do begin Caption := cItem.FirstName; SubItems.Add(cItem.LastName); SubItems.Add(cItem.MiddleName); SubItems.Add(cItem.Gender); SubItems.Add(cItem.Birthday); SubItems.Add(cItem.Email1Address); SubItems.Add(cItem.Email1AddressType); SubItems.Add(cItem.HomeTelephoneNumber); end; except end; end; if Created then Outlook := unassigned; end; procedure TForm1.FormCreate(Sender: TObject); begin PageControl1.ActivePageIndex := 0; end; end. |
Form代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
object Form1: TForm1 Left = 450 Top = 238 BorderIcons = [biSystemMenu] BorderStyle = bsDialog Caption = 'Outlook reader test' ClientHeight = 604 ClientWidth = 985 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False Position = poScreenCenter OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object btnGetData: TButton Left = 16 Top = 568 Width = 161 Height = 25 Caption = 'Get Data From Outlook' TabOrder = 0 OnClick = btnGetDataClick end object PageControl1: TPageControl Left = 8 Top = 8 Width = 961 Height = 545 ActivePage = TabSheet2 TabOrder = 1 object TabSheet1: TTabSheet Caption = 'Appointment' object lvCalendar: TListView Left = 0 Top = 0 Width = 953 Height = 517 Align = alClient Columns = < item Caption = 'Subject' Width = 80 end item Caption = 'Start' Width = 80 end item Caption = 'End' Width = 80 end item Caption = 'Duration' Width = 80 end item Caption = 'Location' Width = 80 end item Caption = 'Body' Width = 80 end> GridLines = True TabOrder = 0 ViewStyle = vsReport end end object TabSheet2: TTabSheet Caption = 'Contact' ImageIndex = 1 object lvContact: TListView Left = 0 Top = 0 Width = 953 Height = 517 Align = alClient Columns = < item Caption = 'First Name' Width = 90 end item Caption = 'Last Name' Width = 90 end item Caption = 'M Name' Width = 90 end item Caption = 'Gender' Width = 90 end item Caption = 'BirthDay' Width = 90 end item Caption = 'Email1 Addr' Width = 90 end item Caption = 'Email1 Type' Width = 90 end item Caption = 'HomeTelePhoneNumber' Width = 90 end> GridLines = True TabOrder = 0 ViewStyle = vsReport end end end end |
相关链接:
Microsoft Outlook Constants
AppointmentItem Object Members
Items Members (Outlook)
Folders Property
Attachment Object Members
Application Object Members
ContactItem Object Members