Oversigt
I denne artikel beskrives det, hvordan du bruger Microsoft Outlook 10.0-objektbiblioteket til at hente bestemte meddelelser ved hjælp af metoden Søg og metoden Begræns i Microsoft Visual C#.
Flere oplysninger
Hvis du vil bruge Microsoft Outlook 10.0-objektbibliotek til at hente bestemte meddelelser, skal du følge disse trin:
-
Start Microsoft Visual Studio .NET eller Microsoft Visual Studio 2005.
-
I menuen Filer skal du pege på Ny og derefter klikke på Project.
-
Klik på Konsolprogram på listen Visual C#Projekttyper.
Filen Class1.cs oprettes som standard.
Bemærk! I Microsoft Visual C# 2005 skal du klikke på Visual C#" på listen Visual C# Projects types . Program.cs oprettes som standard. -
Føj en reference til Microsoft Outlook 10.0-objektbiblioteket. Det gør du ved at følge disse trin:
-
I menuen Project skal du klikke på Add Reference.
-
Klik på fanen COM, find Microsoft Outlook 10.0-objektbibliotek, og klik derefter på Vælg.
Bemærk i Microsoft Visual C# 2005. behøver du ikke at klikke på Vælg. -
Klik på OK i dialogboksen Tilføj referencer.
-
Hvis du bliver bedt om at oprette wrappers for de biblioteker, du har valgt, skal du klikke på Ja.
-
-
Erstat koden i kodevinduet med følgende:
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;
}
}
} -
Søg efter TODO-tekststrengen i koden, og rediger derefter koden for dit miljø.
-
Tryk på F5 for at oprette og køre programmet.
Referencer
Du kan finde flere oplysninger om sikkerhedsfunktionerne i Outlook 2002 ved at klikke på følgende artikelnummer for at få vist artiklen i Microsoft Knowledge Base:
290500 Beskrivelse af udviklerrelaterede mailsikkerhedsfunktioner i Outlook 2002For
yderligere oplysninger om problemer, der kan opstå, når du bruger egenskaben Tæl for store samlinger, skal du klikke på det følgende artikelnummer for at få vist artiklen i Microsoft Knowledge Base:
294385 OL2002: Egenskaben Forkert antal ved hjælp af tilbagevendende aftaler