QuotedStr(’Borland International’));
就是查找CompanyName 属性为Borland International的联系人。如果没有找到匹配的联系人,CurrentContact就为空值。while循环利用FindNext来遍历并匹配联系人,并把联系人的全部属性插入到
数据库中。
创建新的联系人目录和记录也非常简单,下面的代码可以复制全部的Borland公司雇员联系信息到一个新的目录:
procedure TCreateFolderFrom.CreateBtnClick(Sender: TObject);
const
olFolderContacts = 10;
olContactItem = 2;
var
OutlookApp, Mapi,
NewContact, BorlandContacts,
ContactItems, CurrentContact: Variant;
I, ToRemove: Integer;
begin
OutlookApp := CreateOleObject(’Outlook.Application’);
Mapi := OutlookApp.GetNameSpace(’MAPI’);
ContactItems := Mapi.Folders(’个人文件夹’).Folders(’联系人’).Items;
{ 删除测试文件夹 }
ToRemove := 0;
for I := 1 to Mapi.Folders(’Personal Folders’).Folders.Count do
if Mapi.Folders(’个人文件夹’).Folders(I).Name =’Borland 联系人’ then
begin
ToRemove := I;
Break;
end; // if
if ToRemove <> 0 then
Mapi.Folders(’Personal Folders’).Folders.Remove(ToRemove);
{ 创建新的文件夹 }
Mapi.Folders(’个人文件夹’).Folders.Add(’Borland 联系人’, olFolderContacts);
BorlandContacts := Mapi.Folders(’Personal Folders’).Folders(’Borland Contacts’);
{ 添加联系人到新的目录 }
CurrentContact := ContactItems.Find(’[CompanyName] = ’ +
QuotedStr(’Borland International’));
while not VarIsEmpty(CurrentContact) do
begin
{ 添加新的项目 }
NewContact := BorlandContacts.Items.Add;
{ 设定属性 }
NewContact.FullName := ’John Doe’;
NewContact.LastName := CurrentContact.LastName;
NewContact.FirstName := CurrentContact.FirstName;
NewContact.CompanyName := CurrentContact.CompanyName;
NewContact.BusinessAddressStreet :=
CurrentContact.BusinessAddressStreet;
NewContact.BusinessAddressPostOfficeBox :=
CurrentContact.BusinessAddressPostOfficeBox;
NewContact.BusinessAddressCity :=
CurrentContact.BusinessAddressCity;
NewContact.BusinessAddressState :=
CurrentContact.BusinessAddressState;
NewContact.BusinessAddressPostalCode :=
CurrentContact.BusinessAddressPostalCode;
NewContact.BusinessTelephoneNumber :=
CurrentContact.BusinessTelephoneNumber;
{ 保存记录 }
NewContact.Save;
{ 查找联系人目录中下一个记录}
CurrentContact := ContactItems.FindNext;
end; // while
OutlookApp := Unassigned;
end;
上面的代码流程就是先在Folders 集合中查找Borland 联系人目录,如果找到了就调用Folders 的Remove方法删除之。然后调用Folders 对象的Add 方法创建一个新的Borland 联系人文件夹。Add方法需要两个参数:第一个是要创建的目录名;第二个是文件夹类型(可以是olFolderCalendar、olFolderContacts、olFolderInbox、olFolderJournal、olFolderNotes或olFolderTasks类型)。
接下来调用联系人目录的Items对象的Find方法来定位Borland雇员的信息记录。然后调用新建的Borland联系人目录的Items对象的Add方法来添加在联系人目录中找到的记录。最后调用新添记录的Save方法来保存添加的信息。
其他Outlook对象
个人文件夹的Folders 集合还包括下列文件夹:已删除邮件;收件箱;发件箱;已发送邮件;日历;日记;便笺;任务;草稿。
我们可以使用类似的方法来操作任意对象的Items 集合,它们的区别只是集合项目的属性不同,下面的代码演示了如何把约会中的全部起始时间定为大于99/04/27,并且把全天约会的信息复制到
数据库的方法。注意这里使用比前面要复杂的查找表达式,查找表达式支持>,<,>=,<=,=和<>操作符以及and,or和not逻辑操作符。
procedure TLoadTableForm.LoadBtnClick(Sender: TObject);
var
OutlookApp, Mapi,
ApptItems, CurrentAppt: Variant;
begin
OutlookApp := CreateOleObject(’Outlook.Application’);
Mapi := OutlookApp.GetNameSpace(’MAPI’);
pptItems := Mapi.Folders(’Personal Folders’).Folders(’Calendar’).Items;
with ApptTable do
begin
EmptyTable;
Open;
DisableControls;
CurrentAppt := ApptItems.Find(’[Start] > ’ +
"4/27/99" and [AllDayEvent] = True’);
while not VarIsEmpty(CurrentAppt) do
begin
Insert;
FieldByName(’Start’).AsDateTime := CurrentAppt.Start;
FieldByName(’Subject’).AsString := CurrentAppt.Subject;
FieldByName(’End’).AsDateTime := CurrentAppt.End;