現在のOutlookの予約を取得します
質問
現在の予約が必要です。現在の予約がない場合は、次の任命または以前の予約です。
使用すると思います Restrict
任命のセットを制限し、制限引数に応じて最初または最後の任命を選択するには(例:現在の時間以降に終了する予定の制限、または現在の時期に始まる予定)。
引数として必要な文字列フィルターに問題があります。
単純なVB例(コード切り株):
myStart = Format(Date, "mm/dd/yyyy hh:mm AMPM")
strRestriction = "[Start] <= '" & myStart & "'"
'Restrict the Items collection
Set oResItems = oItems.Restrict(strRestriction)
'Sort
oResItems.Sort "[Start]"
C#でも同じことをしようとしています。
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
// Get the NameSpace and Logon information.
// Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("mapi");
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
//Log on by using a dialog box to choose the profile.
oNS.Logon(Missing.Value, Missing.Value, true, true);
// Get the Calendar folder.
Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
// Get the Items (Appointments) collection from the Calendar folder.
oItems = oCalendar.Items;
oItems.IncludeRecurrences = true;
// THIS IS THE PROBLEM AREA
String filter = "[Start] <= '" + DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") + "'";
Outlook.Items restrictedItems = oItems.Restrict(filter);
// Take the last item on the list - should be current or next appointment
restrictedItems.Sort("[Start]");
Outlook.AppointmentItem oAppt = restrictedItems.GetLast();
// Done. Log off.
oNS.Logoff();
フィルターは文字列であるため、日付形式はyyyy/mm/dd hh:mm:ssである必要がありますか? [開始]を操作する方法に関するドキュメントが見つかりません。
日付形式に応じて、すべての予定を除くフィルターのために、間違った予定を取得するか、GetLastを使用できません。
私は例を見てきましたが、予定をループして(非効率的すぎる)か、日付形式が正しい予定を返すことを信頼できないように見えます(たとえば https://social.msdn.microsoft.com/forums/vstudio/en-us/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-appointment-items-from-out-out-2007-calendareStrict?forum = vto, 、使用する場合、代わりに日付がハードコードされているようです DateTime.Now
.)
更新:現在、以下に示すようにループしています。より効率的なコードに関する提案はありますか?
DateTime currentTime = DateTime.Now;
foreach (Outlook.AppointmentItem item in oItems)
{
if (item.Start <= currentTime && item.End.Subtract(new TimeSpan(0, 10, 0)) > currentTime)
{
appointmentArrayList.Add(item);
}
}
正しい解決策はありません
所属していません StackOverflow