Podsumowanie
W tym artykule opisano sposób używania biblioteki obiektów programu Microsoft Outlook 10.0 do pobierania określonych wiadomości przy użyciu metod Znajdź i Ogranicz w języku Microsoft Visual C#.
Więcej informacji
Aby pobrać określone wiadomości Outlook biblioteki obiektów programu Microsoft Outlook 10.0, wykonaj następujące czynności:
-
Uruchom Microsoft Visual Studio .NET lub Microsoft Visual Studio 2005.
-
W menu Plik wskaż polecenie Nowy, a następnie kliknij polecenie Project.
-
Na liście typów projektów Visual C# kliknij pozycję Aplikacja konsoli.
Domyślnie jest tworzony plik Class1.cs.
Uwaga W programie Microsoft Visual C# 2005 kliknij pozycję Visual C#" na liście typów projektów w języku Visual C #. Domyślnie jest tworzony program.cs. -
Dodaj odwołanie do biblioteki obiektów programu Microsoft Outlook 10.0. W tym celu wykonaj następujące czynności:
-
W menu Projekt kliknij polecenie Dodaj odwołanie.
-
Kliknij kartę COM, znajdź pozycję Microsoft Outlook 10.0 Object Library, a następnie kliknij przycisk Wybierz.
Uwaga: Microsoft Visual C# 2005. nie musisz klikać przycisku Wybierz. -
W oknie dialogowym Dodawanie odwołań kliknij przycisk OK.
-
Jeśli zostanie wyświetlony monit o wygenerowanie zawijaczy dla wybranych bibliotek, kliknij przycisk Tak.
-
-
W oknie kodu zamień kod na następujący:
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;
}
}
} -
Wyszukaj ciąg tekstowy TODO w kodzie, a następnie zmodyfikuj kod dla środowiska.
-
Naciśnij klawisz F5, aby utworzyć i uruchomić program.
Informacje
Aby uzyskać więcej informacji o funkcjach zabezpieczeń programu Outlook 2002, kliknij następujący numer artykułu, aby wyświetlić ten artykuł w bazie wiedzy Microsoft Knowledge Base:
290500 Opis funkcji zabezpieczeń poczty e-mail związanych z deweloperami w programie Outlook 2002Dla
dodatkowych informacji o problemach, które mogą wystąpić podczas używania właściwości Liczba dużych kolekcji, kliknij następujący numer artykułu, aby wyświetlić ten artykuł w bazie wiedzy Microsoft Knowledge Base:
294385 OL2002: Właściwość Niepoprawna liczba przy użyciu terminów cyklicznych