Microsoft KB Archive/313805

From BetaArchive Wiki
Knowledge Base


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:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects types list, click Console Application.

    By default, Module1.vb is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate the Microsoft Outlook 10.0 Object Library, and then click Select.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. 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("MAPI")
            oNS.Logon("", "", 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("Totall : " & oItems.Count)
    
            Dim sSearch As String
            ' Use Restrict method
            sSearch = "[Start] >= '03/01/2004 08:00 PM' and [Start] <= '03/30/2004 08:00 AM'"
            oItems = oItems.Restrict(sSearch)
            Console.WriteLine("Totall Restricted : " & 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() + ": " + oAppointment.Subject.ToString())
            Next
    
            ' Use Find method
            sSearch = "[Start] >= '03/01/2004 08:00 PM' and [Start] <= '03/30/2004 08:00 AM'"
            '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("The following apppoitment is found:")
                Console.WriteLine(oAppointment.Subject.ToString())
            Else
                Console.WriteLine("No appointment is found!")
            End If
    
            ' Logoff
            oNS.Logoff()
    
            ' Clean Up
            oApp = Nothing
            oNS = Nothing
            oItems = Nothing
            oAppointment = Nothing
        End Sub
    End Module
  6. Search for TODO in the code, and then modify the code for your environment.
  7. Press F5 to build and to run the program.
  8. Make sure that the appointments were retrieved.


REFERENCES

For more information, visit the following MSDN Web sites:

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

292451 OL2002: Restrict may not work with uncached properties



Additional query words: outlook restrict find calendar appointment

Keywords: kbhowto kbhowtomaster kboutlookobj KB313805