gpt4 book ai didi

c# - Outlook 2003 - 显示第一次出现的开始/结束日期的重复项

转载 作者:行者123 更新时间:2023-11-30 13:04:35 26 4
gpt4 key购买 nike

背景:我正在用 C# (.NET 3.5) 编写一个应用程序,它查看多个用户的 Outlook 2003 日历(使用 COM 对象),获取约会并为这些约会插入数据入数据库。

问题:在第一个用户日历之后,后续日历上的任何重复项目将始终具有该项目第一次出现的开始和结束时间。我在用户之间发布 COM 对象(并且在此期间,如果用户有很多项目),并且项目集合被正确限制(因为只插入了少数重复性任务(尽管启动错误/结束)而不是来自“无止境”任务的无限)。正确的开始/结束时间是要求的一部分,为这个或另一个应用程序提供信息,以计算出用户在给定日期范围和工作时间范围内有多少空闲时间。

代码:(省略了变量声明,它们在相关函数的顶部)

遍历用户(在 Main() 中):

 foreach (DataRow drUserCalendar in dtCalendars.Rows)
{
//for each calendar we're looking at, export their calendar data and put it in the database
try
{
appOutlook = new Outlook.Application();
ExportCalendar(drUserCalendar);
Marshal.FinalReleaseComObject(appOutlook);
GC.Collect();
}
catch (Exception ex)
{
//report error
}
}

从日历中提取信息

static void ExportCalendar(DataRow drUser)
{
strDisplayName = drUser["DisplayName"].ToString();
strUserID = drUser["ID"].ToString();

int.TryParse(drUser["PreviousDays"].ToString(), out intPrevious);
int.TryParse(drUser["FutureDays"].ToString(), out intFuture);

dtmAllowedPreviousStart = DateTime.Now.AddDays(-intPrevious);
dtmAllowedFutureStart = DateTime.Now.AddDays(intFuture);

nsOne = appOutlook.GetNamespace("MAPI");
nsOne.Logon(null, null, false, false);
rcpOne = nsOne.CreateRecipient(strDisplayName);

intCount = 0;

if (rcpOne.Resolve())
{
fldOne = nsOne.GetSharedDefaultFolder(rcpOne, Outlook.OlDefaultFolders.olFolderCalendar);

strRestrict = "[Start] > '" + MIN_START_DATE.ToString("g") + "' And [End] < '" + MAX_START_DATE.ToString("g") + "'";
itms = fldOne.Items;
itms.Sort("[Start]", Type.Missing);
itms.IncludeRecurrences = true;
itmsRestricted = itms.Restrict(strRestrict);
itmsRestricted.Sort("[Start]", Type.Missing);
itmsRestricted.IncludeRecurrences = true;
blnIsRecurring = false;
dicRecurringTaskTracker = new Dictionary<string, int>();

foreach (object objOne in itmsRestricted)
{

if (intCount >= 100 || blnIsRecurring)
{
//release COM objects. Outlook doesn't like you having more than 250 ish items without cleaning up.
Marshal.FinalReleaseComObject(appOutlook);
appOutlook = new Outlook.Application();
GC.Collect();
intCount = 0;
}

if (objOne is Outlook.AppointmentItem)
{
appItem = (Outlook.AppointmentItem)objOne;
blnException = false;

//get data from the item
strEntryID = appItem.EntryID;
strSubject = appItem.Subject;
strBody = appItem.Body;
dtmStart = appItem.Start;
dtmEnd = appItem.End;

blnException = EXCEPTIONS.Contains(strSubject);

//if the item is an exception we're done with it.
if (!blnException)
{
strRecurrenceInterval = "";
strRecurrenceType = "";
strRecurrenceInfo = "";


//check if it's a recurring task.
blnIsRecurring = appItem.IsRecurring;
if (blnIsRecurring)
{
//check to see if we've already had a task from this series
if (!dicRecurringTaskTracker.Keys.Contains(strEntryID))
{
//Start at 0 so the first (this) task
//is number 1.
dicRecurringTaskTracker.Add(strEntryID, 0);
}

//update number
dicRecurringTaskTracker[strEntryID] += 1;
//change the subject to add the count on the end
strEntryID = strEntryID + '-' + dicRecurringTaskTracker[strEntryID].ToString();

//it's a recurring task, so we need to find out when/how often.
rpTaskRecurrence = appItem.GetRecurrencePattern();
rtTaskRecurrenceType = rpTaskRecurrence.RecurrenceType;
strRecurrenceType = rtTaskRecurrenceType.ToString();
strRecurrenceInterval = rpTaskRecurrence.Interval.ToString();

switch (strRecurrenceType)
{
case "olRecursDaily":
case "olRecursMonthNth":
case "olRecursWeekly":
strRecurrenceInfo = rpTaskRecurrence.DayOfWeekMask.ToString();
break;
case "olRecursMonthly":
strRecurrenceInfo = rpTaskRecurrence.DayOfMonth.ToString();
break;
}
}

if (strEntryID != null && strSubject != null && dtmStart != null && dtmEnd != null
&& (intPrevious == 0 || (dtmStart > dtmAllowedPreviousStart)) && (intFuture == 0 || (dtmStart < dtmAllowedFutureStart)))
{
//build up the SQL
strSQL = "EXEC UpdateCalendarEntry ";
strSQL += "@EntryID='" + strEntryID + "', ";
strSQL += "@Subject='" + strSubject.Replace("'", "''") + "', ";
strSQL += "@Body='" + strSubject.Replace("'", "''") + "', ";
strSQL += "@StartDate='" + dtmStart.ToString("dd-MMM-yyyy HH:mm:ss") + "', ";
strSQL += "@EndDate='" + dtmEnd.ToString("dd-MMM-yyyy HH:mm:ss") + "', ";
strSQL += "@UserCalendarID=" + strUserID + ",";
strSQL += "@Recurring = " + blnIsRecurring.ToString() + ",";
strSQL += "@RecurrenceType = '" + strRecurrenceType + "',";
strSQL += "@RecurrenceInterval = '" + strRecurrenceInterval + "',";
strSQL += "@RecurrenceInfo = '" + strRecurrenceInfo + "';";

try
{
//Execute SQL
}
catch (Exception ex)
{
//Print error message
MessageBox.Show(ex.ToString());
}
}
}
Marshal.FinalReleaseComObject(appItem);
GC.Collect();
}
strEntryID = null;
strSubject = null;
strBody = null;
intCount++;
}

//finished looping, do some clean up.
Marshal.FinalReleaseComObject(nsOne);
Marshal.FinalReleaseComObject(rcpOne);
Marshal.FinalReleaseComObject(fldOne);
Marshal.FinalReleaseComObject(itms);
Marshal.FinalReleaseComObject(itmsRestricted);
GC.Collect();
}
else
{
throw new Exception("Could not resolve name");
}
}

最佳答案

恐怕我看不出您的代码存在明显的问题,但我希望您很清楚,可能在幕后发生了一些其他事情导致了您的问题。

我在博客文章 - http://jynxeddevelopment.blogspot.com 中介绍了我在使用这些东西时发现的一些最佳实践。 .可能值得一读,看看是否有什么与您正在做的不同,我认为“保留对所有内容的引用”部分可能会有用。

我不确定您的 COM 对象是否会被 GC 调用收集,因为您没有首先将它们设置为 null,但是无论哪种方式,这都不会有什么不同。在没有任何 GC 调用的情况下,我得到了这种东西。

要看的东西:

  • 如果 objOne 是一个 COM 对象(我希望它是),它应该在每个循环中释放
  • 在发布之前关闭您的 Outlook 应用程序 (appOutlook.Close()),我很惊讶您没有让很多应用程序闲逛
  • 检查您在 COM 对象上使用的每个字段,如果它们也是 COM 对象,它们可能也需要完成

抱歉,这不是具体的,但使用这些东西是一项艰苦的工作:/祝你好运!

-金克斯

关于c# - Outlook 2003 - 显示第一次出现的开始/结束日期的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8355960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com