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 Basic .NET.
Module Module1 Sub Main() End Sub End ModuleNote When you use the Write method or the WriteLine method to write to the console, you can add a breakpoint to the End Sub line of the code so that you can read the console before the console closes.
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objFolder As Outlook.MAPIFolder objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Console.Write(objFolder.Name) End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objFolder As Outlook.MAPIFolder = _ objNS.Folders.Item("Public Folders") objFolder = objFolder.Folders.Item("All Public Folders") objFolder = objFolder.Folders.Item("My Public Folder") Console.Write(objFolder.Name) End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objMessage As Outlook._MailItem objMessage = objOutlook.CreateItem(Outlook.OlItemType.olMailItem) Dim objFolder As Outlook.MAPIFolder = objMessage.Parent Console.Write(objFolder.Name) End Subback to the top
GetSharedDefaultFolder: Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objRecipient As Outlook.Recipient = _ objNS.CreateRecipient("John Smith") If objRecipient.Resolve Then Dim objFolder As Outlook.MAPIFolder = _ objNS.GetSharedDefaultFolder(objRecipient, _ Outlook.OlDefaultFolders.olFolderCalendar) Console.Write(objFolder.Name) Else Console.Write("Recipient could not be resolved.") End If End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objFolder As Outlook.MAPIFolder objFolder = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) objFolder = objFolder.Parent objOutlook.ActiveExplorer.CurrentFolder = objFolder End SubFor additional information about how to access other people's folders, click the following article number to view the article in the Microsoft Knowledge Base:
290824 How to open someone else's calendar or other folder
back to the top293152 Programming with EntryIDs and StoreIDs
back to the topFolders.Add Method: Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objTasks As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks) Dim objFolder As Outlook.MAPIFolder = _ objTasks.Folders.Add("Personal Tasks", _ Outlook.OlDefaultFolders.olFolderTasks) End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objTask As Outlook._TaskItem objTask = objOutlook.CreateItem(Outlook.OlItemType.olTaskItem) objTask.Subject = "Test Item" objTask.Display() End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objContacts As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objItems As Outlook._Items = objContacts.Items Dim objContact As Outlook._ContactItem = objItems.Add("IPM.Contact.MyForm") objContact.Display() End SubThe following example uses the Items.Add method to create a new item in the Contacts folder that is based on the default form for the folder. You can set the default form by changing the When posting to this folder, use setting in the folder Properties dialog box:
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objContacts As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objItems As Outlook._Items = objContacts.Items Dim objContact As Outlook._ContactItem = objItems.Add objContact.Display() End SubNote 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 it has been published in the folder, in the personal forms library, or in the organizational forms library.
290657 Working with form definitions and one-off forms
290659 OL2002: How to update existing items to use a new custom form
back to the topSub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objContacts As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objContact As Outlook._ContactItem = _ objOutlook.CreateItemFromTemplate("C:\Contact.oft") Dim objForm As Outlook.FormDescription = objContact.FormDescription objForm.Name = "My Contact" objForm.PublishForm(Outlook.OlFormRegistry.olFolderRegistry, objContacts) End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objContacts As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objItems As Outlook._Items = objContacts.Items Dim iCount As Int16 = objItems.Count Dim i As Int16 For i = 1 To iCount Console.WriteLine(objItems.Item(i).Fullname) Next End SubThe following example uses the For Each...Next construct to loop through all the contacts in the Contacts folder and to write the FullName field to the console window:
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objContacts As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objItems As Object = objContacts.Items Dim objItem As Object For Each objItem In objItems Console.WriteLine(objItem.FullName) Next End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objInbox As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) Dim objItems As Outlook._Items = objInbox.Items Dim objMessage As Outlook._MailItem = objItems.Item("Please help on Friday!") objMessage.Display() End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objFolder As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks) Dim objTasks As Outlook._Items = objFolder.Items Dim objTask As Outlook._TaskItem = _ objTasks.Find("[Importance] = 'High'") If objTask Is Nothing Then MsgBox("Nothing Important. Go party!") Else MsgBox("You have something important to do!") End If End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objNS As Outlook._NameSpace = objOutlook.Session Dim objFolder As Outlook.MAPIFolder = _ objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) Dim objContacts As Outlook._Items = objFolder.Items Dim strCriteria As String = _ "[CompanyName] = 'A. Datum Corporation'" Dim objADatumitems As Object = objContacts.Restrict(strCriteria) Dim objADatumitem As Outlook.ContactItem For Each objADatumitem In objADatumitems Console.WriteLine(objADatumitem.FullName) Next End Subback to the top
Sub Main() Dim objOutlook As Outlook._Application objOutlook = New Outlook.Application() Dim objSelection As Outlook.Selection = _ objOutlook.ActiveExplorer.Selection Dim iCount As Int16 = objSelection.Count Dim i As Int16 For i = iCount To 1 Step -1 Console.WriteLine(objSelection.Item(i).Subject) Next End Subback to the top
293152 Programming with EntryIDs and StoreIDs
back to the top287530 Questions about custom forms and Outlook solutions
back to the top