Outlook Interop C#排序项目不起作用 [英] Outlook Interop c# sort items not working

查看:52
本文介绍了Outlook Interop C#排序项目不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了一个问题,其中Outlook项目表排序方法无法提供期望的结果-尽管方法GetLast()升序或降序总是返回相同的电子邮件项目.代码如下:

I have stumbled upon a problem where the Outlook items table sort method does not give desired results - despite the ascending or descending the method GetLast() always returns the same email item. Code as follows:

Application olApp = new Application();
NameSpace olNS = olApp.GetNamespace("MAPI");
MAPIFolder oFolder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

Explorer oExp = oFolder.GetExplorer(false);
//olNS.Logon( false, true);

result = new IOActionResult(null);

oFolder.Items.Sort("[ReceivedTime]");

var subject = oFolder.Items.GetLast().Subject;

我尝试指定以下内容:

oFolder.Items.Sort("[ReceivedTime]", true);
oFolder.Items.Sort("[ReceivedTime]", false);
oFolder.Items.Sort("[ReceivedTime]", OlSortOrder.olAscending);
oFolder.Items.Sort("[ReceivedTime]", OlSortOrder.olDescending);

似乎也不起作用...任何想法都感激!

Which did not seem to work either... Any thoughts appreciated!

推荐答案

在最后一行;

var subject = oFolder.Items.GetLast().Subject;

您正在从Outlook中返回一个新的Items对象,因此实际上是在您不再引用的实例上执行了排序.

You are being returned a new Items object from Outlook, so your sort was actually performed on an instance that you no longer have a reference to.

将代码更改为以下形式;

Change your code to look like this;

Application  olApp = new Application();
NameSpace olNS = olApp.GetNamespace("MAPI");
MAPIFolder oFolder = olNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

Items items = oFolder.Items;
items.Sort("[ReceivedTime]");

var subject = items.GetLast().Subject;

针对Outlook开发时,一个好的经验法则是始终将对象的中间成员分配给它们自己的局部变量.这与稍后发布它们特别相关.

A good rule of thumb when developing against Outlook is to always assign intermediary members of objects to their own local variable. This is particular relevant for releasing them later on.

这篇关于Outlook Interop C#排序项目不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆