Sammanfattning

I den här artikeln beskrivs hur du använder Microsoft Outlook 10.0-objektbibliotek för att hämta specifika meddelanden med metoden Sök och metoden Begränsa i Microsoft Visual C#.

Mer information

Om du vill använda Microsoft Outlook 10.0-objektbiblioteket för att hämta specifika meddelanden gör du så här:

  1. Starta Microsoft Visual Studio .NET eller Microsoft Visual Studio 2005.

  2. På menyn Arkiv pekar du på Nytt och klickar sedan på Project.

  3. Klicka på Konsolprogram i listan Visuella C#Projekttyper. Filen Class1.cs skapas som standard.Obs! I Microsoft Visual C# 2005 klickar du på Visual C#" i listan Med Visual C# Projekttyper . Som standard skapas Program.cs.

  4. Lägg till en referens till Microsoft Outlook 10.0-objektbibliotek. Följ de här anvisningarna om du vill göra det:

    1. Project klickar du på Lägg till referens.

    2. Klicka på fliken COM, leta reda på Microsoft Outlook 10.0-objektbibliotek och klicka sedan på Markera. Obs! I Microsoft Visual C# 2005. behöver du inte klicka på Markera.

    3. Klicka på OK i dialogrutan Lägg till referenser.

    4. Om du uppmanas att generera radbrytningar för de valda biblioteken klickar du på Ja.

  5. Ersätt koden i kodfönstret med följande:

    using System;using System.Reflection;     // to use Missing.Valuenamespace FilterAppointments{   /// <summary>   /// Summary description for Class1.   /// </summary>   class Class1   {      /// <summary>      /// The main entry point for the application.      /// </summary>      [STAThread]      public static int Main(string[] args)      {         try         {            // Create an Outlook application.            Outlook.Application oApp = new Outlook.Application();            // Get the Mapi NameSpace and the Logon.            Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");            // Log on using dialog to choose a profile.            oNS.Logon(Missing.Value, Missing.Value, true, true);             // Alternate Logon using specific profile            // TODO: Change the profile name where it is appropriate.            //oNS.Logon("YourValidProfile", Missing.Value, false, true);             // Get the Calendar folder.            Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);            // Get the Items collection in the folder.            Outlook.Items  oItems = (Outlook.Items)oCalendar.Items;            Console.WriteLine("Total Items (unrestricted): " + oItems.Count);            //Include all occurrences of recurring items, and then sort them.            oItems.Sort ("[Start]", false);            oItems.IncludeRecurrences = true;            // Define the string for the search criteria.            String sCriteria;            // Set the criteria for the Date fields.            sCriteria = "[Start] <= '09/01/2002 08:00 AM' and [End] >= '2/15/2002 08:00 PM'";            // Set the criteria for a string field.            // sCriteria = "[Subject] = 'Weekly recurring meeting'";            // Set the criteria for a numeric field.            // Look for meetings that have not been responded to.            // sCriteria = "[ResponseStatus] = " + (Int32)Outlook.OlResponseStatus.olResponseNotResponded)            // Use the Restrict method to reduce the number of items to process.            Outlook.Items oRestrictedItems = oItems.Restrict(sCriteria);            oRestrictedItems.Sort ("[Start]", false);            oRestrictedItems.IncludeRecurrences = true;            Console.WriteLine("Total Items Unrestricted : " + oRestrictedItems.Count);            Outlook.AppointmentItem oAppointment;            //Get each item until item is null.            Outlook.AppointmentItem oAppt;            oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetFirst();            while (oAppt != null)            {               // Console.WriteLine(i.ToString());                Console.WriteLine("  Subject: " + oAppt.Subject.ToString());                Console.WriteLine("  Start time: " + oAppt.Start.ToString());                Console.WriteLine("  End time: " + oAppt.End.ToString());                Console.WriteLine("  Occurrences: " + oAppt.RecurrenceState.ToString());                Console.WriteLine("\n\n");                oAppt = (Outlook.AppointmentItem)oRestrictedItems.GetNext();            }            // Use the Find method to get single match.            sCriteria = "[Start] >= '09/30/2001'";             Console.WriteLine("Criteria: " + sCriteria.ToString());            oAppointment = (Outlook.AppointmentItem)oItems.Find(sCriteria);            Console.WriteLine("Used Find with Date fields");            if (oAppointment is Outlook.AppointmentItem)                Console.WriteLine("Found -> " + oAppointment.Subject.ToString());            else               Console.WriteLine("No object found");            // Log off            oNS.Logoff();            // Clean up            oAppointment = null;            oRestrictedItems = null;            oItems = null;            oCalendar = null;            oNS = null;            oApp = null;         }            //Simple error handling         catch (Exception e)         {            Console.WriteLine("{0} Exception caught.", e);         }           //Default return value         return 0;      }   }}
  6. Sök efter TODO-textsträngen i koden och ändra sedan koden för din miljö.

  7. Tryck på F5 för att skapa och köra programmet.

Referenser

Om du vill ha mer information om säkerhetsfunktionerna i Outlook 2002 klickar du på följande artikelnummer för att visa artikeln i Microsoft Knowledge Base:

290500 Beskrivning av de utvecklarrelaterade säkerhetsfunktionerna för e-post i Outlook 2002Tillför ytterligare information om problem som kan uppstå när du använder egenskapen Antal för stora samlingar klickar du på följande artikelnummer för att visa artikeln i Microsoft Knowledge Base:

294385 OL2002: Egenskapen Felaktigt antal med återkommande avtalade tider

Behöver du mer hjälp?

Vill du ha fler alternativ?

Utforska prenumerationsförmåner, bläddra bland utbildningskurser, lär dig hur du skyddar din enhet med mera.