Pregunta

Necesito la cita actual. Si no hay cita actual, la siguiente o incluso la cita anterior.

Me imagino usar Restrict Para limitar el conjunto de citas y luego elegir la primera o última cita dependiendo del argumento de restricción (por ejemplo, restringir a las citas que terminan después de la hora actual, o las citas que comienzan antes de la hora actual).

Tengo problemas con el filtro de cadena necesario como argumento.

Un ejemplo simple de VB (código de código):

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

Estoy intentando hacer lo mismo en 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();

Me imagino que dado que el filtro es una cadena, el formato de fecha debe ser aaa yyy/mm/dd hh: mm: ss? No puedo encontrar ninguna documentación sobre cómo manipular el [inicio], como analizarla a una fecha o algo así.

Dependiendo del formato de fecha, obtendré la cita incorrecta o no podré usar Getlast debido al filtro que excluye todas las citas.

He visto ejemplos, pero o recorren las citas (demasiado ineficientes), o los formatos de fecha parecen no se les puede confiar para devolver la cita correcta (por ejemplo. https://social.msdn.microsoft.com/forums/vstudio/en-us/c6a8bd21-6534-43be-b23e-1068651da92e/retrieve-apointment-itemss-outlook-2007-calendar-restrict?, que parece tener la fecha codificada en su lugar si se usa DateTime.Now.)

ACTUALIZACIÓN: Actualmente estoy recorriendo como se muestra a continuación. ¿Alguna sugerencia para un código más eficiente?

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

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top