Domanda

Ho bisogno dell'attuale appuntamento. Se nessun appuntamento corrente, il prossimo o addirittura il precedente appuntamento.

Immagino da usare Restrict Per limitare il set di appuntamenti, quindi scegliere il primo o l'ultimo appuntamento a seconda dell'argomento limitato (ad esempio, limitare gli appuntamenti che si concludono dopo l'ora corrente o gli appuntamenti a partire dall'ora corrente).

Ho problemi con il filtro stringa necessario come argomento.

Un semplice esempio VB (Suptum di codice):

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]"

Sto tentando di fare lo stesso in 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();

Immagino poiché il filtro è una stringa, il formato della data deve essere yyyy/mm/dd hh: mm: ss? Non riesco a trovare alcuna documentazione su come manipolare il [inizio], come analizzarlo ad una data o qualcosa del genere.

A seconda del formato della data, otterrò l'appuntamento errata o non sarò in grado di utilizzare GetLast a causa del filtro escluso tutti gli appuntamenti.

Ho visto esempi, ma o si aggirano attraverso gli appuntamenti (troppo inefficienti), o i formati della data sembrano che non ci si possa fidare per restituire l'appuntamento corretto (ad esempio https://social.msdn.microsoft.com/forums/vstudio/en-us/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-appointment-items-from-outlook-2007-calendar-restrict?forum=vSto, che sembra avere la data hardcode invece se si utilizza DateTime.Now.)

AGGIORNAMENTO: Attualmente sto passando come mostrato di seguito. Qualche suggerimento per un codice più efficiente?

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

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top