2011-02-06 2 views
3

私は、予定の見通しを確認するプログラムを作成しています。基本的には現在の予定が必要です。現在の予定がない場合は、次の予定または前の予定です。現在のOutlook予定を取得する

私はこれを行うと思います[restrictメソッド] [1]を使用して予定のセットを制限し、制限引数に応じて最初または最後の予定のいずれかを選択する必要があります。現在の時刻の後、または現在の時刻より前に開始する予定のみ)。 引数として必要な文字列フィルタに多くの問題があります。次のように

単純なVBの例(コード断端)は([ソース] [2])である

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#で同じことをしようとすると動作していないようです。 MM:SSフィルターは日付形式はYYYY/MM/DD HHにする必要があり、文字列であるため、すべての

// 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(); 

まず、私が想像しますか?そして、私は[Start]を操作する方法に関するドキュメントを見つけられないようです(日付や何かに構文解析するなど)。私が使用している日付形式によっては、間違ったアポイントメントを取るか、すべての予定を除外したフィルタのためにプログラムがGetLastを使用できなくなります。

Googleはもはや私を助けてくれるはずがありません。私は仕事をしていると主張している人々の例をいくつか見てきましたが、予定がループしすぎている(非効率的です)か、正しいアポイントメントを返すことができるようにしてください(social.msdn.microsoft.com/Forums/en-US/vsto/thread/c6a8bd21-6534-43be-b23e-1068651da92eのように、使用している場合は日付を代わりにハードコードしているようですDateTime.Now ..)

UPDATE:Restrictアングルを試してみたいと思っていたので、現在は以下のようにループしていますが、より効率的なコード大歓迎です。 toString呼び出しのためのフォーマット文字列として:「MM YYYY-MM-DD HH」の情報に従うことにより

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); 
    } 
} 

答えて

2

は、私が使用して動作するようにそれを得ることができた、hereを発見しました。

これが役に立ちます。

+0

どうもありがとうございます!すべてが今のように機能するようです。私は月に首都Mを忘れてしまったようです:S – Madsn

3

これはあなたの問題です:私はあなたのためになるだろうと思います何

DateTime.Now.ToString("MM/dd/yyyy hh:mm AMPM") 

は次のとおりです。

DateTime.Now.ToString("MM/dd/yyyy hh:mm tt", CultureInfo.InvariantCulture) 
0

このコードは、今日以降からOutlookの予定を表示するように動作します:

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     DemoAppointmentsInRange(); 
    } 

    private void DemoAppointmentsInRange() 
    { 
     Application a = new Application(); 
     Microsoft.Office.Interop.Outlook.Folder calFolder = a.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderCalendar) as Microsoft.Office.Interop.Outlook.Folder; 
     DateTime start = DateTime.Now; 
     DateTime end = start.AddDays(5); 
     Microsoft.Office.Interop.Outlook.Items rangeAppts = GetAppointmentsInRange(calFolder, start, end); 
     if (rangeAppts != null) 
     { 
      foreach (Microsoft.Office.Interop.Outlook.AppointmentItem appt in rangeAppts) 
      { 
       Response.Write("Subject: " + appt.Subject + " "+" Start: "+appt.Start.ToString()+" "+"End:"+appt.End.ToString()+"<br/>"); 
      } 
     } 
    } 
    private Microsoft.Office.Interop.Outlook.Items GetAppointmentsInRange(
    Microsoft.Office.Interop.Outlook.Folder folder, DateTime startTime, DateTime endTime) 
    { 
     string filter = "[Start] >= '"+ startTime.ToString("g")+ "' AND [End] <= '" + endTime.ToString("g") + "'"; 
     //Response.Write(filter); 
     try 
     { 
      Microsoft.Office.Interop.Outlook.Items calItems = folder.Items; 
      calItems.IncludeRecurrences = true; 
      calItems.Sort("[Start]", Type.Missing); 
      Microsoft.Office.Interop.Outlook.Items restrictItems = calItems.Restrict(filter); 
      if (restrictItems.Count > 0) 
      { 
       return restrictItems; 
      } 
      else 
      { 
       return null; 
      } 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 
+0

あなたのコードが何をしているか説明してください。 –

+0

今日からOutlook予定を示しています。期待どおりに機能しています。 –

関連する問題