简体   繁体   English

如何读取 outlook 的自定义字段值

[英]How to read Custom Field value of outlook

Can anyone tell me how to read the custom Field value of the outlook using c#谁能告诉我如何使用 c# 读取 outlook 的自定义字段值

Right now i tried "UserProperties" and "ItemProperties".现在我尝试了“UserProperties”和“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 reply might be a bit late for you.这个回复对你来说可能有点晚了。 But I came across the same problem as the original poster (need to extract values from User-Defined Fields (Custom fields) of Outlook).但我遇到了与原始海报相同的问题(需要从 Outlook 的用户定义字段(自定义字段)中提取值)。

Someone somewhere shared a VBA macro which did the job.有人在某处分享了一个 VBA 宏来完成这项工作。 So I went around looking for a way to execute Outlook macro VBA in powershell. As it turns out Outlook stopped supporting /autorun of macro after version 2003 SP2.所以我四处寻找在 powershell 中执行 Outlook 宏 VBA 的方法。事实证明 Outlook 在 2003 SP2 版本之后停止支持 /autorun 宏。 I nearly switched from Outlook 2019 to Outlook 2003 just for the /autorun.我差点从 Outlook 2019 切换到 Outlook 2003 只是为了 /autorun。 Then it occurred to me Outlook 2003 isn't as good with showing multiple calendars side by side as Outlook 2019. So I decided to keep going with 2019 afterall.然后我想到 Outlook 2003 并排显示多个日历不如 Outlook 2019 好。所以我决定继续使用 2019 年。 Cut long story short, I massaged the VBA macro to run in Powershell and it worked for me.长话短说,我修改了 VBA 宏以在 Powershell 中运行,它对我有用。 I basically added in a few dollar signs in front of things, and removed some stuff here and there, and then it just worked.我基本上在事物前面添加了几个美元符号,并在这里和那里删除了一些东西,然后它就起作用了。 I hope it works for everyone else too who is still looking for answer:我希望它也适用于仍在寻找答案的其他人:

Function Get-ContactUDF
{
 Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
 $Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
 $objNameSpace = $Outlook.GetNameSpace("MAPI")
 $objContacts  = $objNameSpace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
 $objContact = $objContacts.Items.Find("[FirstName] = ""John""")
 $objProperty = $objContact.UserProperties.Find("Favorite Character in Lord of the Rings")
 $objProperty.Value
}

Just in case the code is not clear.以防万一代码不清楚。 This powershell function will look for the Contact whose FirstName is "John", and then find the value under the custom field called "Favorite Character in Lord of the Rings" and print out the value (for example Frodo).这个 powershell function 将查找 FirstName 为“John”的联系人,然后在名为“指环王中最喜欢的角色”的自定义字段下找到值并打印出该值(例如 Frodo)。 To run it, ofcourse you just do this under powershell:要运行它,当然你只需在 powershell 下执行此操作:

Get-ContactUDF

By the way if you just want to print values from default fields (those built-in fields that came with Outlook eg. Firstname, Surname, Birthday, phone number, spouse name, title etc), then it's like the following:顺便说一句,如果您只想打印默认字段(Outlook 附带的那些内置字段,例如名字、姓氏、生日、电话号码、配偶姓名、头衔等)的值,则如下所示:

Function Get-OutlookContacts
{
 Add-Type -assembly "Microsoft.Office.Interop.Outlook" -ErrorAction Stop -ErrorVariable "OutlookError"
 $Outlook = New-Object -comobject Outlook.Application -ErrorAction stop -ErrorVariable "ApplicationError"
 $namespace = $Outlook.GetNameSpace("MAPI")
 $contactObject  = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderContacts)
}

Call this function by just typing:只需键入以下内容即可调用此 function:

Get-OutlookContacts

in Powershell在 Powershell

THIS WILL LIST EVERYTHING IN THE CONTACTS DATABASE:这将列出联系人数据库中的所有内容:

$contactObject.Items

THIS WILL LIST ALL OBJECTS FIRSTNAME, LASTNAME, BIRTHDAY, EMAILADDRESS, MOBILE IN THE CONTACTS DATABASE:这将在联系人数据库中列出所有对象的名字、姓氏、生日、电子邮件地址、手机:

$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber

ISSUE this next command to grab objects and list only for where the firstname object equals Rambo:发出下一个命令以抓取对象并仅列出名字 object 等于 Rambo 的地方:

$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber | where-object {$_.Firstname -eq "Rambo" }

Hope these examples are clear.希望这些例子很清楚。 I use Outlook 2019 on Windows 10 totally offline mode (no cloud, no 365).我使用 Outlook 2019 on Windows 10 完全离线模式(没有云,没有 365)。 If you use 365 then the code above probably needs modifications.如果您使用 365,那么上面的代码可能需要修改。

I also remember other people asking for a way to extract Outlook Calendar events using Powershell. If I come across that question again I'll post the answer.我还记得其他人要求使用 Powershell 提取 Outlook 日历事件的方法。如果我再次遇到这个问题,我会发布答案。 Basically the same drill.基本上是一样的钻。 I read some VB code from somewhere and then turned it into Powershell script.我从某处读取了一些 VB 代码,然后将其转换为 Powershell 脚本。 You guys can do it yourself if you like.如果你愿意,你们可以自己做。 Just add in dollar signs and remove the DIM lines.只需添加美元符号并删除 DIM 行。

This answer has been rewritten following experiments with Outlook.在使用 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.我的 C# 足以让我知道你在做什么,但我还没有尝试从 C# 访问 Outlook。

Both Items and Item are used within the Outlook model. ItemsItem都在 Outlook 模型中使用。 I do not know it you can use item in the way you are attempting.我不知道您可以按照您尝试的方式使用item

UserProperties would throw an error if user property "test" did not exist.如果用户属性“test”不存在,UserProperties 将抛出错误。 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.下面显示了从 Outlook VBA 访问用户属性。 I would expect the InterOp model to be as similar as the syntax allows.我希望 InterOp 模型与语法允许的一样相似。 Important differences of which I know:我知道的重要区别:

  • Set is not required with C#. C# 不需要Set
  • Nothing is the VBA equivalent of null . Nothing是 VBA 等效的null

Variables:变量:

  • FolderItem is an item within a folder that has been tested to be of class olMail. FolderItem 是文件夹中的一项,经测试属于 olMail 类。
  • UserProp is of type UserProperty. UserProp 是 UserProperty 类型。
  • InxUP is of type integer. InxUP 是整数类型。

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.下面添加名为“TestProp”和类型 olText 的用户属性,将其值设置为“TestProp 的值”并保存修改后的邮件项。 Without the Save, the previous statements have no effect.如果没有 Save,前面的语句就没有效果。

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.下面检查用户属性“TestProp”是否存在,如果存在,则将其值输出到即时窗口。

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

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM