Samenvatting
In dit artikel wordt beschreven hoe u de Objectbibliotheek van Microsoft Outlook 10.0 gebruikt om specifieke berichten op te halen met behulp van de methode Zoeken en de methode Beperken in Microsoft Visual C#.
Meer informatie
Als u de Objectbibliotheek van Microsoft Outlook 10.0 wilt gebruiken om specifieke berichten op te halen, gaat u als volgt te werk:
-
Start Microsoft Visual Studio .NET of Microsoft Visual Studio 2005.
-
Wijs in het menu Bestand de knop Nieuw aan en klik op Project.
-
Klik in de lijst Visual C# Projects op Consoletoepassing.
Standaard wordt het bestand Class1.cs gemaakt.
Opmerking Klik in Microsoft Visual C# 2005 op Visual C#" in de lijst Visual C# Projects types . Standaard wordt Program.cs gemaakt. -
Voeg een verwijzing naar de Objectbibliotheek van Microsoft Outlook 10.0 toe. Volg de volgende stappen om dit te doen:
-
Klik op Verwijzing toevoegen in het menu Project.
-
Klik op het tabblad COM, zoek Microsoft Outlook 10.0 Objectbibliotheek en klik vervolgens op Selecteren.
Opmerking in Microsoft Visual C# 2005. u hoeft niet op Selecteren te klikken. -
Klik in het dialoogvenster Verwijzingen toevoegen op OK.
-
Als u wordt gevraagd om wrappers te genereren voor de bibliotheken die u hebt geselecteerd, klikt u op Ja.
-
-
Vervang de code in het codevenster door het volgende:
using System;
using System.Reflection; // to use Missing.Value
namespace 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;
}
}
} -
Zoek de tekenreeks TODO in de code en wijzig vervolgens de code voor uw omgeving.
-
Druk op de F5-toets om het programma te maken en uit te voeren.
Verwijzingen
Voor meer informatie over de beveiligingsfuncties van Outlook 2002, klikt u op het volgende artikelnummer om het artikel te bekijken in de Microsoft Knowledge Base:
290500 Beschrijving van de beveiligingsfuncties voor ontwikkelaars in Outlook 2002
Voor meer informatie over problemen die kunnen optreden wanneer u de eigenschap Aantal van grote verzamelingen gebruikt, klikt u op het volgende artikelnummer om het artikel te bekijken in de Microsoft Knowledge Base:
294385 OL2002: onjuiste eigenschap Aantal met terugkerende afspraken