Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
Programming samples that can reference items and folders in Outlook by using Visual C# .NET
Article ID: 310244 - View products that this article applies to.
This article was previously published under Q310244
The Microsoft Outlook object model is generally used to access various types of items in folders. This article contains samples of the methods, the properties, and the objects that you can use to refer to Outlook items and Outlook folders by using Microsoft Visual C# .NET.
This article contains the following topics and their associated samples:
To add the sample code to start the application, follow these steps:
Referencing existing folders
GetDefaultFolder methodThe default folders are those folders that are at the same level as your Inbox that receives your incoming mail. If you have more than one Inbox in your profile, you can press CTRL+SHIFT+I to select your default Inbox. The default folders are those folders that you work with regularly, such as the Calendar folder, the Contacts folder, and the Tasks folder. You can easily refer to these folders by using the GetDefaultFolder method. The GetDefaultFolders method takes one argument that defines the type of folder that you want to refer to. An Outlook object has an enumeration that you can select. This enumeration is defined in the Outlook.OlDefaultFolders enum type. The following sample code assigns the oFolder object variable to the default Inbox folder.
Folders objectYou can use the Folders object to refer to any folder that is in the Outlook folder list. The Folders object is typically used to refer to a Microsoft Exchange folder or to any other folder that is not a default Outlook folder.
The following sample code illustrates how to refer to a public folder that is named My Public Folder. Notice that you typically start at the top-most folder and then work your way down to the folder that you have to reference. Also notice that the folder names are case-sensitive. You must exactly match the capitalization and the names as they appear in the Outlook folders list.
Parent propertyIf you already have a reference to an Outlook item or to an Outlook folder, you can use the Parent property of the Outlook item or of the Outlook folder to create a reference to the folder that the item or the folder is located in.
The following sample code returns the name of a folder for a particular item.
GetSharedDefaultFolder methodYou can use this method if another person has granted you permission to one of their default folders. You use the GetSharedDefaultFolder method as you use the GetDefaultFolder method, except that you must specify one additional argument. The one additional argument is the name of the folder that belongs to the other person. This is the folder that you want to reference. The following sample code resolves the name of the other person to verify that the name of the other person is valid, and that the name of the other person can be used with the GetSharedDefaultFolder method.
For more information about how to access information in folders that belong to other people, click the following article number to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/290824/ )How to open another user's calendar or another folder in Outlook 2002
GetFolderFromID methodThis method is typically used with more complex solutions where a solution keeps track of both the EntryID field and the StoreID field of a folder so that the folder can be quickly referenced later. For additional information, see article 293152 in the "References" section.
Creating new folders and referencing new folders
Folders.Add methodWhen you use the Folders.Add method by using the Add method of the Folders collection, you can create a new folder. The first argument specifies the name of the folder, and the second argument specifies the type of the folder. The following sample code adds a Personal Tasks subfolder to your default Tasks folder.
Creating new items and referencing new items
CreateItem methodThe CreateItem method creates a new default Outlook item. If you have to create an item that is based on a custom form that you have created, use the Items.Add method that is discussed in the next sample. You can find the CreateItem method located in the top-level application object in the Outlook object model. The CreateItem method requires only one argument. This one argument is a constant that indicates the type of item to create.
Items.Add methodWhen you use the Add method on the Items collection, you can create a new item that is based on any message class. The message class may be a default Outlook message class, such as the IPM.Contact message class, or a message class for a custom form, such as the IPM.Contact.MyForm message class. To use the Items.Add method, you must first reference the folder where you want to create a new item.
For more information about message classes, click the following article numbers to view the articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/290657/ )Description of form definitions and one-off forms in Outlook 2002
290659The following sample code uses the Items.Add method to create a new item that is based on a custom contact form that is named MyContact.
(http://support.microsoft.com/kb/290659/ )How to update existing items to use a new custom form
The following sample code uses the Items.Add method to create a new default contact item.
Note If you use the Items.Add method, the default form for the folder is not important. You can specify any valid message class as long as the message class has been published in the folder, in the personal forms library, or in the organizational forms library.
CreateItemFromTemplate methodYou can use the CreateItemFromTemplate method to create a new item that is based on an Outlook template file (.oft) format or on a message file (.msg) format. Because most forms are published in a folder or in a forms library, this method is not generally used. However, you may want to use this method to create a Setup program that is used to install forms for an Outlook solution. You typically do this for users who do not have network access or for users who work offline in Outlook. You want to use the Setup program for the following reasons:
Referencing existing items
Using Items(I) methodYou can use the Using Items(I) method to loop through all the items in a folder. The Items collection contains all the items in a particular folder. You can specify the item that you want to reference by using an index with the Items collection. This is typically used with the For loop construct.
The following sample code uses the Items(I) method approach to loop through all the contacts in the Contacts folder and then to display their FullName field in a dialog box.
Using Items("Subject") methodYou can use the Using Items("Subject") method with the Items collection. You do this to specify a test string that matches the Subject field of an item.
The following sample code displays an item in the Inbox with a subject line that contains "You must help on Friday!"
Find methodYou can use the Find method to search for an item in a folder. The search is based on the value of one of the fields for the item. If the Find method is successful, you can then use the FindNext method to look for additional items that meet the same criteria.
The following sample code searches to see if you have any high priority tasks.
Restrict methodThe Restrict method is similar to the Find method. However, instead of returning a single item, the Restrict method returns a collection of items that meet the search criteria. For example, you can use this method to find all contacts who work for the same company.
The following sample code writes the FullName property of all the contacts who work for the A. Datum Corporation.
GetItemFromID methodYou can use the GetItemFromID method in more complex solutions where you want the solution to keep track of both the EntryID field and the StoreID field of an item. You want to do this so that the item can be quickly referenced later. For additional information, see article 293152 in the "References" section.
For more information about how to use the GetFolderFromID method, click the following article number to view the article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/293152/ )Programming with EntryIDs and StoreIDs
Article ID: 310244 - Last Review: March 15, 2006 - Revision: 5.5