在我使用 Microsoft.Office.Interop.Outlook 的測驗 C# 應用程式中。我正在嘗試僅接收指定日期的日歷事件。下面的過濾器等于:
[Start] >= '11/13/2022 00:00:00 AM' AND [END] <= '11/14/2022 11:59:59 PM'
但我遇到了兩個問題。排序似乎不起作用,第二個問題是在未來日期取消約會并在早上早些時候跳過約會。

...
DateTime dtFrom = new DateTime(DateTime.Now.Year, 11, 14);
DateTime dtTo = new DateTime(DateTime.Now.Year, 11, 14);
Recipient teamMember = _app.Session.CreateRecipient("persons_email_address");
teamMember.Resolve();
if (teamMember.Resolved)
{
MAPIFolder sharedCalendar = _app.Session.GetSharedDefaultFolder(teamMember, OlDefaultFolders.olFolderCalendar);
if (sharedCalendar.DefaultMessageClass != "IPM.Appointment" || teamMember.DisplayType != 0)
{
return; //Calendar not shared.
}
var sdf = dtFrom.ToString("MM/dd/yyy") " 00:00:00 AM";
var sdt = dtTo.ToString("MM/dd/yyy") " 11:59:59 PM";
string restrictCriteria = $"[Start] >= '{sdf}' AND [END] <= '{sdt}'";
Items items = sharedCalendar.Items;
items.Sort("[Start]");
items.IncludeRecurrences = true;
var results = items.Restrict(restrictCriteria);
for (int i = 1; i <= results.Count; i )
{
try
{
AppointmentItem item = sharedCalendar.Items[i];
Console.WriteLine("Item: {0}", i.ToString());
Console.WriteLine("Subject: {0}", item.Subject);
Console.WriteLine("Start: {0}", item.Start);
//Console.WriteLine("Start: {0}", item.SenderName);
Console.WriteLine("End: {0}", item.End);
Console.WriteLine("Categories: {0}", item.Categories);
}
catch (System.Exception err)
{
}
}
uj5u.com熱心網友回復:
您需要根據 Outlook 要求更改條件(小于Start和大于)并設定日期格式。End以下代碼在我的機器上就像一個魅力:
using System.Diagnostics;
using System.Runtime.InteropServices;
// ...
private void RestrictCalendarItems(Outlook.MAPIFolder folder)
{
DateTime dtEnd = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
DateTime.Now.Day, 23, 59, 00, 00);
string restrictCriteria = "[Start]<=\"" dtEnd.ToString("g") "\""
" AND [End]>=\"" DateTime.Now.ToString("g") "\"";
StringBuilder strBuilder = null;
Outlook.Items folderItems = null;
Outlook.Items resultItems = null;
Outlook._AppointmentItem appItem = null;
int counter = default(int);
object item = null;
try
{
strBuilder = new StringBuilder();
folderItems = folder.Items;
folderItems.IncludeRecurrences = true;
folderItems.Sort("[Start]");
resultItems = folderItems.Restrict(restrictCriteria);
item = resultItems.GetFirst();
do
{
if (item != null)
{
if (item is Outlook._AppointmentItem)
{
counter ;
appItem = item as Outlook._AppointmentItem;
strBuilder.AppendLine("#" counter.ToString()
"\tStart: " appItem.Start.ToString()
"\tSubject: " appItem.Subject
"\tLocation: " appItem.Location);
}
Marshal.ReleaseComObject(item);
item = resultItems.GetNext();
}
}
while (item != null);
if (strBuilder.Length > 0)
Debug.WriteLine(strBuilder.ToString());
else
Debug.WriteLine("There is no match in the "
folder.Name " folder.");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
finally
{
if (folderItems != null) Marshal.ReleaseComObject(folderItems);
if (resultItems != null) Marshal.ReleaseComObject(resultItems);
}
}
你可以看看我為技術博客寫的文章 - How To: Use Restrict method in Outlook to get calendar items。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/533356.html
上一篇:在最小API中設定身份驗證端點類
