引用文件夹中的现有项目
使用 Microsoft Visual Basic 引用文件夹中的现有项目有多种方法。 本主题提供有关以下方面的信息:
使用 For … Next 或 For Each … Next 循环
这些语句通常用于循环访问文件夹中的所有项目。 Items 集合包含特定文件夹中的所有项目,可以用 Items 集合的索引指定要引用的项目。 通常与 For i = 1 to n
编程构造一起使用。
使用 For Each...Next
在未指定索引的情况下循环访问集合中的项。 这两种方法所得到的结果相同。
以下示例使用 For…Next
循环访问“联系人”文件夹中的所有联系人,并在对话框中显示“全名”字段。
' Microsoft Visual Basic for Applications code example.
Set olns = Application.GetNameSpace("MAPI")
' Set MyFolder to the default contacts folder.
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
' Get the number of items in the folder.
NumItems = MyFolder.Items.Count
' Set MyItem to the collection of items in the folder.
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
' Loop through all of the items in the folder.
For I = 1 to NumItems
MsgBox MyItems(I).FullName
Next
' Visual Basic Scripting Edition code example.
Set olns = Item.Application.GetNameSpace("MAPI")
' Set MyFolder to the default contacts folder.
Set MyFolder = olns.GetDefaultFolder(10)
' Get the number of items in the folder.
NumItems = MyFolder.Items.Count
' Set MyItem to the collection of items in the folder.
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
' Loop through all of the items in the folder.
For I = 1 to NumItems
MsgBox MyItems(I).FullName
Next
以下示例使用 For Each...Next
实现与前面的示例相同的结果:
' Visual Basic/Visual Basic for Applications code example.
Set olns = Application.GetNameSpace("MAPI")
' Set MyFolder to the default contacts folder.
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
' Set MyItems to the collection of items in the folder.
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
For Each SpecificItem in MyItems
MsgBox SpecificItem.FullName
Next
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
' Set MyFolder to the default contacts folder.
Set MyFolder = olns.GetDefaultFolder(10)
' Set MyItem to the collection of items in the folder.
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
For Each SpecificItem in MyItems
MsgBox SpecificItem.FullName
Next
使用 Items 集合
也可以使用 Items 集合并指定与项目的"主题"字段匹配的文本字符串。 以下示例显示“收件箱”中主题中包含“Please help on Friday!”的项目。
' Visual Basic/Visual Basic for Applications code example.
Set olns = Application.GetNameSpace("MAPI")
' Set MyFolder to the default Inbox.
Set MyFolder = olns.GetDefaultFolder(olFolderInbox)
Set MyItem = MyFolder.Items("Please help on Friday!")
MyItem.Display
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
' Set MyFolder to the default Inbox.
Set MyFolder = olns.GetDefaultFolder(6)
Set MyItem = MyFolder.Items("Please help on Friday!")
MyItem.Display
使用 Find 方法
使用 Find 方法根据项目某个字段的值在文件夹中搜索项目。 如果搜索成功,则可使用 FindNext 方法检查符合相同搜索条件的其他项目。
以下示例搜索是否存在高优先级的任务。
' Visual Basic/Visual Basic for Applications code example.
Set olns = Application.GetNamespace("MAPI")
Set myFolder = olns.GetDefaultFolder(olFolderTasks)
Set MyTasks = myFolder.Items
' Importance corresponds to Priority on the task form.
Set MyTask = MyTasks.Find("[Importance] = ""High""")
If MyTask Is Nothing Then ' the Find failed
MsgBox "Nothing important. Go party!"
Else
MsgBox "You have something important to do!"
End If
' VBScript code example.
Set olns = Item.Application.GetNamespace("MAPI")
Set myFolder = olns.GetDefaultFolder(13)
Set MyTasks = myFolder.Items
' Importance corresponds to Priority on the task form.
Set MyTask = MyTasks.Find("[Importance] = ""High""")
If MyTask Is Nothing Then ' the Find failed
MsgBox "Nothing important. Go party!"
Else
MsgBox "You have something important to do!"
End If
使用 Restrict 方法
Restrict 方法与 Find 方法相似,但是它返回符合查找条件的项目集合,而不是单个项目。 例如,可以用此方法查找在同一公司工作的所有联系人。
以下示例显示了在 ProseWare Corporation 工作的所有联系人:
' Automation code example.
Set olns = Application.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
MyClause = "[CompanyName] = ""ProseWare"""
Set MyPWItems = MyItems.Restrict(MyClause)
For Each MyItem in MyPWItems
MyItem.Display
Next
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(10)
Set myItems = myFolder.Items.Restrict("[MessageClass] = 'IPM.Contact'")
MyClause = "[CompanyName] = ""ProseWare"""
Set MyPWItems = MyItems.Restrict(MyClause)
For Each MyItem in MyPWItems
MyItem.Display
Next
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。
反馈
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:提交和查看相关反馈