将Outlook.Items从DLL返回到MFC,然后在MFC中进行迭代

时间:2019-05-07 11:44:54

标签: c# outlook mfc office-interop items

这是我当前的功能,它是与Outlook互操作的C#DLL的一部分:

public long PurgePTSCalendar(bool bPurgeAll)
{
    long lCount = 0;
    int iLastPercent = 0;

    if (!IsValid())
        return 0;

    try
    {
        // Get the MAPI namespace object (not sure exactly what this is)
        Outlook.NameSpace oMAPI = _OutlookApp.GetNamespace("MAPI");
        if (oMAPI == null)
            return 0;

        // Now get the default Outlook calendar folder
        Outlook.MAPIFolder oFolder = oMAPI.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
        if (oFolder == null)
            return 0;

        // Ensure it is a calendar folder.  This test is not strictly required.
        if (oFolder.DefaultItemType != Outlook.OlItemType.olAppointmentItem)
            return 0;

        // Get the collect of items from the calendar folder
        Outlook.Items oFolderItems = oFolder.Items;
        if (oFolderItems == null)
            return 0;

        Outlook.Items oItemsToPurge = null;

        // Do we want to purge all entries?
        if (bPurgeAll)
        {
            // Yes, purge all.
            oItemsToPurge = oFolderItems;
        }
        else
        {
            // No, we only want to purge records from today onwards
            // We must get a restricted list
            oFolderItems.Sort("Start");

            // Filter from today onwards (not any start / end 
            // date from a meeting or other item).
            String strStartDate = DateTime.Now.Date.ToShortDateString();
            string strFilter = "[Start] >=\"" + strStartDate + " 12:00 AM\"";
            oItemsToPurge = oFolderItems.Restrict(strFilter);
        }

        if (oItemsToPurge == null)
            return lCount;

        int iNumItems = oItemsToPurge.Count;

        for (int iItem = iNumItems; iItem >= 1; iItem--)
        {
            object oItem = oItemsToPurge[iItem];
            Outlook.AppointmentItem oEvent = (oItem as Outlook.AppointmentItem);

            int iPercent = ((iNumItems - iItem) + 1) * 100 / iNumItems;
            if (iPercent >= iLastPercent + 5 || iPercent == 100)
            {
                iLastPercent = iPercent;
                // Do Progress
            }

            if (oEvent != null)
            {
                // Get category for this item
                string strCat = oEvent.Categories;

                // Can we delete it?
                if (strCat == "xxx")
                {
                    oEvent.Delete();
                    lCount++;
                }
                Marshal.ReleaseComObject(oEvent);
            }
            Marshal.ReleaseComObject(oItem);
        }
        Marshal.ReleaseComObject(oItemsToPurge);
        Marshal.ReleaseComObject(oFolderItems);
        Marshal.ReleaseComObject(oFolder);
        Marshal.ReleaseComObject(oMAPI);
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }

    return lCount;
}

它在我的MFC可执行文件中这样调用:

long COutlookManagerEx::PurgePTSCalendar(bool bPurgeAll)
{
    __int64 lCount = 0;

    if (IsValid())
    {
        VARIANT_BOOL vbPurgeAll = VARIANT_FALSE;
        if (bPurgeAll)
            vbPurgeAll = VARIANT_TRUE;

        throw_if_fail(m_pInterface->PurgePTSCalendar(vbPurgeAll, &lCount));
    }

    return static_cast<long>(lCount);
}

它没问题。只是它缺少进度监控。我试图(通过另一个问问question来了解有关从DLL中更新CDialog中的进度条的信息,但我不理解我在遗憾地提供了答案中的链接。

因此,我试图简化。我的DLL是否有可能返回oItemsToPurge对象?然后从MFC中循环这个对象并从那里执行删除操作?

如果我可以遍历MFC中的列表,然后在删除过程中进行删除操作,则可以更新进度条...

0 个答案:

没有答案