Article ID: 180696
Article Last Modified on 2/12/2007
Referencing Existing Folders
Creating and Referencing New Folders
Creating and Referencing New Items>
Referencing Existing Items
' 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 Object:
You can use the Folders object to refer to any folder that is visible in
the Outlook folder list. This object is typically used to refer to an
Exchange public folder or any other folder that is not a default Outlook
folder.
' 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")
The following examples illustrate how you can refer to a folder called
"Business Tasks," which is a subfolder of the default Tasks 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")
Parent Property:
' 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.ParentGetSharedDefaultFolder:
160302 OL97: How to Open Someone Else's Calendar or Other Folder
' 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:
170991 OL97: Programming with EntryIDs, StoreIDs and GetItemFromID
' 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")
' 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.DisplayItems.Add Method:
176567 OL97: Working with Form Definitions and One-Off Forms
170301 OL97: How to Update Existing Items to Use a New Custom Form
' 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
The following examples use the Items.Add method to create a new default
contact item:
' 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
NOTE: If you use the Items.Add method, it does not matter what the default
form for the folder is. You can specify any valid message class as long as
it has been published in the folder or has been published in the personal
or organizational forms library.
' 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
' 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
The following examples use the For Each...Next construct to achieve the
same result as the preceding examples:
' 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
Using Items("This is the subject"):
' 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 Method:
' 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 Method:
' 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 Method:
170991 OL97: Programming with EntryIDs, StoreIDs and GetItemFromID
166368OL97: How to Get Help Programming with Outlook
170783OL97: Q&A: Questions about Customizing or Programming Outlook
Additional query words: OutSol OutSol97
Keywords: kbhowto kbprogramming KB180696