Microsoft Outlook 97 物件模型通常用來存取各種類型的資料夾中的項目。本文提供各種方法、 屬性和可以用來參照 Outlook 項目和資料夾的物件的概觀。
本文摘要說明下列主題:
參考現有的資料夾
- GetDefaultFolder 方法
- 資料夾物件
- 父屬性
- GetSharedDefaultFolder 方法
- GetFolderFromID 方法
建立和參考新資料夾
建立和參考新的項目 >
- CreateItem 方法
- Items.Add 方法
- CreateItemFromTemplate 方法
參考現有的項目
- 使用 Items(I)] 或 [依每...下一步
- 使用項目 ([這是主旨])
- 找出方法
- 限制的方法
Microsoft 僅,為了說明提供程式設計範例,不提供任何明示或默示的保證,包括但不是限於適售性以及適合某特定用途之默示擔保責任。本文假設您已熟悉我們所示範的程式設計語言,以及用來建立和偵錯程序的工具。Microsoft 技術支援工程師可以協助解釋特定程序的功能,但它們不會修改這些範例以提供附加功能或建構程序,以符合您的特定需求。
如果您有限制程式設計經驗,您可以連絡 Microsoft 認證合作夥伴或 Microsoft 諮詢服務。如需詳細資訊請造訪下列 Microsoft] 網站:
Microsoft 認證合作夥伴-
https://partner.microsoft.com/global/30000104
(https://partner.microsoft.com/global/30000104)
Microsoft 摘要報告服務-
http://support.microsoft.com/gp/advisoryservice
(http://support.microsoft.com/gp/advisoryservice)
如需有關可用的支援選項,以及有關如何連絡 Microsoft 的詳細資訊,請造訪下列 Microsoft 網站:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
(http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)
附註: Visual 基本指令碼版 (VBScript) 程式碼必須使用 Outlook 物件程式庫中定義的常數的數值。您可以在 [Microsoft Outlook 物件程式庫說明檔 (Vbaoutl.hlp) 下 Microsoft Outlook 常數主題中找到這些值的清單。
參考現有的資料夾
GetDefaultFolder 方法:
預設資料夾是那些位於相同的層級會接收傳入郵件的收件匣。如果您的設定檔中有一個以上的 [收件匣],按下 CTRL + SHIFT + I 一定會選取預設 [收件匣]。預設資料夾是大多數使用者使用如 [行事曆]、 [連絡人],] 和 [工作] 資料夾的定期上。您可以輕易地參考使用 GetDefaultFolder 方法這些資料夾。GetDefaultFolder 會是您想要用來參照資料夾類型的一個引數。下列範例會將物件變數 MyFolder 指派給預設的 [連絡人] 資料夾:
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(10)
資料夾物件: 您可以用 Folders 物件來參考任何 Outlook 資料夾清單] 中顯示的資料夾。此物件通常用來參照 Exchange 公用資料夾或任何其他不是一個預設的 Outlook 資料夾的資料夾。
下列範例說明如何參考稱為 「 我公用資料夾 」 的公用資料夾。請注意您通常在最上層資料夾開始,並且執行您的方式向您需要參考該資料夾。也請注意資料夾名稱會區分大小寫,而且必須在 Outlook 資料夾清單中所顯示的樣子完全符合名稱。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyFolder1 = olns.Folders("Public Folders")
Set MyFolder2 = MyFolder1.Folders("All Public Folders")
Set MyFolder3 = MyFolder2.Folders("My Public Folder")
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyFolder1 = olns.Folders("Public Folders")
Set MyFolder2 = MyFolder1.Folders("All Public Folders")
Set MyFolder3 = MyFolder2.Folders("My Public Folder")
下列範例說明如何可以參考一個名為"商務工作",這是預設的 [工作] 資料夾的子資料夾的資料夾。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyTasksFolder = olns.GetDefaultFolder(olFolderTasks)
Set MyFolder = MyTasksFolder.Folders("Business Tasks")
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyTasksFolder = olns.GetDefaultFolder(13)
Set MyFolder = MyTasksFolder.Folders("Business Tasks")
父屬性:
如果您已經參照到 Outlook 項目或資料夾,您可用來建立該資料夾的參考項目及其 Parent 屬性,或資料夾位於。
下列範例會傳回特定項目的資料夾名稱:
' Automation code example.
Set ol = New Outlook.Application
Set MyItem = ol.CreateItem(olMailItem) ' Create new item.
MyItem.Save ' Save it to Inbox.
Set MyFolder = MyItem.Parent ' MyFolder = Inbox.
' VBScript code example.
' Returns the folder of the current item.
Set MyFolder = Item.Parent
GetSharedDefaultFolder:
如果有人給您,可以使用這個方法委派權限給其中一個預設資料夾。在另外您必須有已經連接到 Outlook 中資料夾按一下按一下 [開啟特殊資料夾 [檔案] 功能表,然後按一下 [Exchange Server 資料夾。
如需有關如何存取其他人的資料夾的詳細資訊,請參閱 「 Microsoft 知識庫 」 中下列文:
160302?
(http://support.microsoft.com/kb/160302/EN-US/
)
OL97: 如何開啟別人的行事曆] 或 [其他] 資料夾
一旦 Outlook 設定為使用其他人的資料夾,GetSharedDefaultFolder 方法用於相同的方式為 GetDefaultFolder,除了您指定一個額外的引數--其他人的資料夾名稱要參考。本範例首先會解析其他人的名稱以確認它可以搭配 GetSharedDefaultFolder 方法是有效的名稱。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set myRecipient = olns.CreateRecipient("John Smith")
myRecipient.Resolve
If myRecipient.Resolved Then
Set JohnFolder = olns.GetSharedDefaultFolder _
(myRecipient, olFolderContacts)
End If
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set myRecipient = olns.CreateRecipient("John Smith")
myRecipient.Resolve
If myRecipient.Resolved Then
Set JohnFolder = olns.GetSharedDefaultFolder _
(myRecipient, 10)
End If
GetFolderFromID:
這個方法會通常只能用在更複雜的解決方案其中方案會追蹤的 「 StoreID 」 和 「 EntryID 的資料夾,以便在稍後可快速參考。
如需有關使用 GetFolderFromID 方法的詳細資訊,請參閱 「 Microsoft 知識庫 」 中下列文件:
170991?
(http://support.microsoft.com/kb/170991/EN-US/
)
EntryIDs、 StoreIDs 與 GetItemFromID 程式設計 OL97:
建立和參考新資料夾
Folders.Add 方法:
在資料夾集合上使用 Add 方法可讓您建立新的資料夾。第一個引數指定資料夾的名稱,第二個引數指定的資料夾類型。以下範例新增預設工作資料夾下的 [商務工作] 資料夾。因為未指定資料夾類型則它會繼承上層資料夾的類型。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyTasksFolder = olns.GetDefaultFolder(olFolderTasks)
Set MyNewFolder = MyTasksFolder.Folders.Add("Business Tasks")
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyTasksFolder = olns.GetDefaultFolder(13)
Set MyNewFolder = MyTasksFolder.Folders.Add("Business Tasks")
建立和參考新的項目
CreateItem 方法:
CreateItem 方法建立新的預設 Outlook 項目。如果需要建立依據您所建立的自訂表單項目使用下面的 Items.Add 方法。CreateItem 方法是很方便位於關閉的頂部層級應用程式物件,Outlook 物件模型中。此方法採用只有一個引數常數,指出要建立之項目的類型。
' Automation code example.
Set ol = New Outlook.Application
Set MyTaskItem = ol.CreateItem(olTaskItem)
MyTaskItem.Display
' VBScript code example.
Set MyTasktem = Item.Application.CreateItem(13)
MyTaskItem.Display
Items.Add 方法:
使用上的項目集合的 Add 方法可讓您建立新的項目,根據任何訊息] 類別,無論是預設 Outlook 訊息類別,例如 IPM.Contact 或如 IPM.Contact.MyForm 的一個自訂表單的訊息類別。若要使用 Items.Add 方法,您必須先參考您想要用來建立新的項目的資料夾。
如需有關郵件類別的詳細資訊,請參閱下列文件 「 Microsoft 知識庫 」 中:
176567?
(http://support.microsoft.com/kb/176567/EN-US/
)
OL97: 使用表單定義與一次性表單
170301?
(http://support.microsoft.com/kb/170301/EN-US/
)
OL97: 如何更新現有的項目,若要使用新的自訂表單
下列範例使用 Items.Add 方法可建立新的項目,根據自訂連絡人表單稱為 MyForm:
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set myFolder = olns.GetDefaultFolder(olFolderContacts)
Set MyItem = MyFolder.Items.Add("IPM.Contact.MyForm")
MyItem.Display
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set myFolder = olns.GetDefaultFolder(10)
Set MyItem = MyFolder.Items.Add("IPM.Contact.MyForm")
MyItem.Display
的下列範例使用 Items.Add 方法來建立新的預設連絡人項目:
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set myFolder = olns.GetDefaultFolder(olFolderContacts)
Set MyItem = MyFolder.Items.Add
MyItem.Display
' VBScript code example.
Set olns = Item.Application.GetNameSpace("MAPI")
Set myFolder = olns.GetDefaultFolder(10)
Set MyItem = MyFolder.Items.Add
MyItem.Display
注意: 如果您使用 Items.Add 方法,並不重要資料夾的預設表單是什麼。您可以指定任何有效的訊息類別,只要它已經發佈資料夾中或已發佈個人或組織表單庫中。
CreateItemFromTemplate 方法:
使用 CreateItemFromTemplate 方法來建立新的項目,根據 Outlook 範本檔案 (.oft) 或.msg 檔案格式。因為大部份的表單都有在資料夾或表單庫中發佈,不常使用這個方法。 可能要使用這個方法最常見的原因就是如果您已建立 Microsoft Visual Basic 安裝程式,以安裝 Outlook 方案的表單。這通常會對不具有網路存取權或通常在 Outlook 中離線工作的使用者來完成。Visual Basic 程式會執行下列動作:
- 自動化 Outlook。
- 用以 CreateItemFromTemplate 從網路共用或磁碟片開啟表單。
- 使用 Outlook 物件模型,發佈以供稍後使用表單。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
' Set MyFolder to the default contacts folder.
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
' Set MyItem to an .oft file on a floppy disk.
Set MyItem = ol.CreateItemFromTemplate("A:\Contact.oft")
' Set MyForm to the item Form Description for publishing.
Set MyForm = MyItem.FormDescription
' Name the form, which also sets its message class.
MyForm.Name = "My Contact"
' Publish the folder to the Contacts folder.
MyForm.PublishForm olFolderRegistry, MyFolder
' Close and do not save changes to the item.
MyItem.Close olDiscard
參考現有的項目
使用 Items(I)] 或 [依每...下一步:
通常這種方法用來迴圈處理所有資料夾中的項目。項目集合包含所有特定資料夾中的項目,而且您可以指定若要依據的項目集合中使用索引來參考哪一個項目。這通常使用 [我 = 1 到 n 的程式設計建構。
如果您使用的 VBScript 2.0 (含) 以後版本,您反而可以使用 For Each...Next 的程式設計建構而未指定索引執行迴圈,集合中項目的。這兩種方法達到相同的結果。
下列範例中使用的所有連絡人 [連絡人] 資料夾中執行迴圈,並在一個對話方塊中顯示其 FullName 欄位 Items(I) 方法。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.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
' Loop through all of the items in the folder.
For I = 1 to NumItems
MsgBox MyItems(I).FullName
Next
' VBScript 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
' Loop through all of the items in the folder.
For I = 1 to NumItems
MsgBox MyItems(I).FullName
Next
下列範例使用 For Each...Next 建構以達成與前面範例相同的結果:
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.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
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
For Each SpecificItem in MyItems
MsgBox SpecificItem.FullName
Next
使用 Items("This is the subject"):
您也可以使用 Items 集合並指定與項目] 的 [主旨] 欄位符合的文字字串。不常使用這個方法。
下列範例顯示其主旨包含"請說明星期五!"[收件匣] 中的某個項目
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.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 方法搜尋基礎的其中一個欄位值在資料夾中的項目。如果 [尋找成功,就可以使用 FindNext 方法檢查符合該尋找條件的其他項目。
下列範例搜尋查看是否有任何高優先順序的約會。
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.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 方法類似於 Find] 方法,但傳回而不會傳回單一項目,它符合搜尋準則的項目集合。比方說您可能使用這個方法來尋找在相同公司工作的所有連絡人。
下列範例顯示所有在 ACME 軟體運作的連絡人:
' Automation code example.
Set ol = New Outlook.Application
Set olns = ol.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderContacts)
Set MyItems = MyFolder.Items
MyClause = "[CompanyName] = ""ACME Software"""
Set MyACMEItems = MyItems.Restrict(MyClause)
For Each MyItem in MyACMEItems
MyItem.Display
Next
' VBScript code example.
' Requires VBScript version 2.0 or later.
Set olns = Item.Application.GetNameSpace("MAPI")
Set MyFolder = olns.GetDefaultFolder(10)
Set MyItems = MyFolder.Items
MyClause = "[CompanyName] = ""ACME Software"""
Set MyACMEItems = MyItems.Restrict(MyClause)
For Each MyItem in MyACMEItems
MyItem.Display
Next
GetItemFromID 方法:
這個方法會通常只能用在更複雜的解決方案其中一個解決方案會追蹤的 「 StoreID 」 和 「 EntryID 項目,以便在稍後可快速擷取。
如需有關使用 GetItemFromID 方法的詳細資訊,請參閱 「 Microsoft 知識庫 」 中下列文件:
170991?
(http://support.microsoft.com/kb/170991/EN-US/
)
EntryIDs、 StoreIDs 與 GetItemFromID 程式設計 OL97:
如需有關使用 Microsoft Outlook 97 建立解決方案的詳細資訊,請參閱下列文件 「 Microsoft 知識庫 」 中:
166368?
(http://support.microsoft.com/kb/166368/EN-US/
)
OL97: 如何取得與 Outlook 說明程式設計
170783?
(http://support.microsoft.com/kb/170783/EN-US/
)
OL97: 常見問題: 自訂或程式設計 Outlook 相關問題