フォルダーの既存アイテムを参照する

Visual Basic を使用してフォルダー内の既存のアイテムを参照する方法は多数あります。 ここでは、以下の項目について説明します。

  • または For Each … Next ループのFor … Next使用

  • Items コレクションを使用する

  • Find メソッドを使用する

  • Restrict メソッドを使用する

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 コレクションを使用し、アイテムの "件名" フィールドと一致させる文字列を指定することもできます。 次の例は、件名に "金曜日に手伝って!" という文字列を含むアイテムを受信トレイに表示します。

' 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 のサポートおよびフィードバックを参照してください。