Microsoft KB Archive/313805

= How to use the Find method and the Restrict method to retrieve appointments =

Article ID: 313805

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q313805



SUMMARY
This article describes how to use the Microsoft Outlook 10.0 Object Library or the Microsoft Outlook 11.0 Object Library to retrieve appointments by using the Find method and the Restrict method in Microsoft Visual Basic .NET.



MORE INFORMATION
To retrieve appointments by using the Find method and the Restrict method, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Visual Basic Projects types list, click Console Application.

By default, Module1.vb is created. Add a reference to the Microsoft Outlook 10.0 Object Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate the Microsoft Outlook 10.0 Object Library, and then click Select. In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the code window, replace the code with the following: Imports System.Reflection 'TODO: If you are in Visual Basic .NET 2003, uncomment the following statement. 'Imports Outlook = Microsoft.Office.Interop.Outlook

Module Module1 Sub Main ' Create Outlook Application Dim oApp As Outlook.Application = New Outlook.Application

' Get Mapi NameSpace and Logon Dim oNS As Outlook.NameSpace = oApp.GetNamespace(&quot;MAPI&quot;) oNS.Logon(&quot;&quot;, &quot;&quot;, false, True) ' TODO:

' Get all the appointments from Calendar folder Dim oCal As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar) Dim oItems As Outlook.Items = oCal.Items Console.WriteLine(&quot;Totall : &quot; & oItems.Count)

Dim sSearch As String ' Use Restrict method sSearch = &quot;[Start] >= '03/01/2004 08:00 PM' and [Start] <= '03/30/2004 08:00 AM'&quot; oItems = oItems.Restrict(sSearch) Console.WriteLine(&quot;Totall Restricted : &quot; & oItems.Count)

Dim oAppointment As Outlook.AppointmentItem Dim i As Integer

' Loop each item under Restrict For i = 1 To oItems.Count oAppointment = oItems.Item(i) Console.WriteLine(i.ToString + &quot;: &quot; + oAppointment.Subject.ToString) Next

' Use Find method sSearch = &quot;[Start] >= '03/01/2004 08:00 PM' and [Start] <= '03/30/2004 08:00 AM'&quot; 'The following statement uses the Find method of the Items collection object to        'return the first appointment whose Start field matches the criteria: oAppointment = oItems.Find(sSearch) If Not oAppointment Is Nothing Then Console.WriteLine(&quot;The following apppoitment is found:&quot;) Console.WriteLine(oAppointment.Subject.ToString) Else Console.WriteLine(&quot;No appointment is found!&quot;) End If

' Logoff oNS.Logoff

' Clean Up       oApp = Nothing oNS = Nothing oItems = Nothing oAppointment = Nothing End Sub End Module </li> Search for TODO in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Make sure that the appointments were retrieved.</li></ol>

<div class="references_section">