1

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

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

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);
    }
}

提前致谢

4

2 回答 2

1

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

于 2011-12-18T23:26:19.470 回答
1

这个回复对你来说可能有点晚了。但是我遇到了与原始海报相同的问题(需要从 Outlook 的用户定义字段(自定义字段)中提取值)。

某个地方的某个人共享了一个 VBA 宏来完成这项工作。所以我四处寻找一种在 powershell 中执行 Outlook 宏 VBA 的方法。事实证明,在 2003 SP2 版本之后,Outlook 停止支持 /autorun 宏。我几乎只是为了 /autorun 从 Outlook 2019 切换到 Outlook 2003。然后我想到 Outlook 2003 在并排显示多个日历方面不如 Outlook 2019 好。所以我决定继续使用 2019。长话短说,我按摩了 VBA 宏以在 Powershell 中运行,它对我有用。我基本上在事物前面添加了几个美元符号,并在这里和那里删除了一些东西,然后它就起作用了。我希望它也适用于仍在寻找答案的其他所有人:

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
}

以防万一代码不清楚。此 powershell 函数将查找 FirstName 为“John”的联系人,然后在名为“指环王中最喜欢的角色”的自定义字段下找到值并打印出该值(例如 Frodo)。要运行它,当然你只需在 powershell 下执行此操作:

Get-ContactUDF

顺便说一句,如果您只想从默认字段(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)
}

只需键入以下内容即可调用此函数:

Get-OutlookContacts

在 Powershell 中

这将列出联系人数据库中的所有内容:

$contactObject.Items

这将列出联系人数据库中的所有对象名字、姓氏、生日、电子邮件地址、手机:

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

发出下一个命令来抓取对象并仅列出名字对象等于 Rambo 的位置:

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

希望这些例子很清楚。我在 Windows 10 完全离线模式(无云,无 365)上使用 Outlook 2019。如果您使用 365,那么上面的代码可能需要修改。

我还记得其他人要求使用 Powershell 提取 Outlook 日历事件的方法。如果我再次遇到这个问题,我会发布答案。基本一样的钻。我从某处读取了一些 VB 代码,然后将其转换为 Powershell 脚本。各位喜欢的话可以自己做。只需添加美元符号并删除 DIM 行。

于 2021-10-05T10:54:04.897 回答