如何读取Outlook的“自定义字段”值 [英] How to read Custom Field value of outlook

查看:223
本文介绍了如何读取Outlook的“自定义字段”值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何使用c#读取Outlook的自定义字段值。

现在我尝试了 UserProperties和 ItemProperties。两者都抛出错误。我的示例代码如下:

  Microsoft.Office.Interop.Outlook.Application f =新的Microsoft.Office.Interop.Outlook.Application (); 
NameSpace outlookNS = f.GetNamespace( MAPI);
MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);


foreach(inboxFolder.Items中的对象obj)
{
MailItem item = obj作为MailItem;
if(item!= null)
{
Console.WriteLine(item.UserProperties [ test]。Value);
Console.WriteLine(item.ItemProperties [ test]。Value);
}
}

预先感谢

解决方案

在使用Outlook进行实验后,此答案已被重写。



我的C#对我来说足够好知道您在做什么,但是我还没有尝试从C#访问Outlook。



两个项目 Item 在Outlook模型中使用。我不知道您是否可以在尝试时使用 item



如果用户属性会引发错误,用户属性测试不存在。下面我展示了如何测试存在性。您是否要添加用户属性,而忘记保存修改后的邮件项目?



以下显示了从Outlook VBA对用户属性的访问。我希望InterOp模型与语法允许的相似。我知道其中的重要区别:

C#不需要


  • Set

  • 没什么 null 的VBA等效项。



变量:




  • FolderItem是文件夹中具有以下内容的项目已被测试为olMail类。

  • UserProp的类型为UserProperty。

  • InxUP的类型为整数。



下面添加了一个名称为 TestProp的用户属性,类型为olText,将其值设置为 TestProp的值,并保存了修改后的邮件项。如果没有保存,则前面的语句无效。

  With FolderItem 
Set UserProp = .UserProperties.Add( TestProp,olText)
UserProp.Value = TestProp的值
。保存

结束

以下将立即输出窗口中每个用户属性与邮件项的名称和值。

  For InxUP = 1 To .UserProperties.Count 
Debug.Print User prop& InxUP和_
.UserProperties(InxUP)。名称& & _
.UserProperties(InxUP).Value
下一个

以下检查用户属性 TestProp存在,如果存在,则将其值输出到立即窗口。

 设置UserProp = .UserProperties.Find( TestProp)
如果不是UserProp,则
调试打印 TestProp和.UserProperties( TestProp)。值
如果

希望这样做有帮助


Can anyone tell me how to read the custom Field value of the outlook using c#

Right now i tried "UserProperties" and "ItemProperties". Both are throwing error. My sample code as follows

Microsoft.Office.Interop.Outlook.Application f = new Microsoft.Office.Interop.Outlook.Application();
NameSpace outlookNS = f.GetNamespace("MAPI");
MAPIFolder inboxFolder = outlookNS.GetDefaultFolder(OlDefaultFolders.olFolderInbox);


foreach (object obj in inboxFolder.Items)
{
    MailItem item = obj as MailItem;
    if (item != null)
    {
        Console.WriteLine(item.UserProperties["test"].Value);
        Console.WriteLine(item.ItemProperties["test"].Value);
    }
}

Thanks in advance

解决方案

This answer has been rewritten following experiments with Outlook.

My C# is good enough for me to know what you are doing but I have not tried to access Outlook from C# yet.

Both Items and Item are used within the Outlook model. I do not know it you can use item in the way you are attempting.

UserProperties would throw an error if user property "test" did not exist. Below I show how to test for existence. Are you adding a user property and forgetting to save the amended mail item?

The following shows access to user properties from Outlook VBA. I would expect the InterOp model to be as similar as the syntax allows. Important differences of which I know:

  • Set is not required with C#.
  • Nothing is the VBA equivalent of null.

Variables:

  • FolderItem is an item within a folder that has been tested to be of class olMail.
  • UserProp is of type UserProperty.
  • InxUP is of type integer.

The following adds a user property with name "TestProp" and type olText, sets its value to "Value for TestProp" and saves the amended mail item. Without the Save, the previous statements have no effect.

With FolderItem 
  Set UserProp = .UserProperties.Add("TestProp", olText)
  UserProp.Value = "Value for TestProp"
  .Save 
End with

The following outputs to the immediate window the name and value of every user property against the mail item.

For InxUP = 1 To .UserProperties.Count
  Debug.Print "    User prop " & InxUP & _
                        .UserProperties(InxUP).Name & " " & _
                        .UserProperties(InxUP).Value
Next

The following checks that user property "TestProp" exists and, if so, outputs its value to the immediate window.

Set UserProp = .UserProperties.Find("TestProp")
  If Not UserProp Is Nothing Then
    Debug.Print "    TestProp " & .UserProperties("TestProp").Value
End If

Hope this helps

这篇关于如何读取Outlook的“自定义字段”值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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