[英]How to read Custom Field value of outlook
誰能告訴我如何使用 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);
}
}
提前致謝
這個回復對你來說可能有點晚了。 但我遇到了與原始海報相同的問題(需要從 Outlook 的用戶定義字段(自定義字段)中提取值)。
有人在某處分享了一個 VBA 宏來完成這項工作。 所以我四處尋找在 powershell 中執行 Outlook 宏 VBA 的方法。事實證明 Outlook 在 2003 SP2 版本之后停止支持 /autorun 宏。 我差點從 Outlook 2019 切換到 Outlook 2003 只是為了 /autorun。 然后我想到 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 function 將查找 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)
}
只需鍵入以下內容即可調用此 function:
Get-OutlookContacts
在 Powershell
這將列出聯系人數據庫中的所有內容:
$contactObject.Items
這將在聯系人數據庫中列出所有對象的名字、姓氏、生日、電子郵件地址、手機:
$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber
發出下一個命令以抓取對象並僅列出名字 object 等於 Rambo 的地方:
$contactObject.Items | Select-Object -Property FirstName, LastName, Birthday, Email1Address, MobileTelephoneNumber | where-object {$_.Firstname -eq "Rambo" }
希望這些例子很清楚。 我使用 Outlook 2019 on Windows 10 完全離線模式(沒有雲,沒有 365)。 如果您使用 365,那么上面的代碼可能需要修改。
我還記得其他人要求使用 Powershell 提取 Outlook 日歷事件的方法。如果我再次遇到這個問題,我會發布答案。 基本上是一樣的鑽。 我從某處讀取了一些 VB 代碼,然后將其轉換為 Powershell 腳本。 如果你願意,你們可以自己做。 只需添加美元符號並刪除 DIM 行。
在使用 Outlook 進行實驗后,此答案已被重寫。
我的 C# 足以讓我知道你在做什么,但我還沒有嘗試從 C# 訪問 Outlook。
Items
和Item
都在 Outlook 模型中使用。 我不知道您可以按照您嘗試的方式使用item
。
如果用戶屬性“test”不存在,UserProperties 將拋出錯誤。 下面我將展示如何測試是否存在。 您是否添加了用戶屬性並忘記保存修改后的郵件項目?
下面顯示了從 Outlook VBA 訪問用戶屬性。 我希望 InterOp 模型與語法允許的一樣相似。 我知道的重要區別:
Set
。Nothing
是 VBA 等效的null
。變量:
下面添加名為“TestProp”和類型 olText 的用戶屬性,將其值設置為“TestProp 的值”並保存修改后的郵件項。 如果沒有 Save,前面的語句就沒有效果。
With FolderItem
Set UserProp = .UserProperties.Add("TestProp", olText)
UserProp.Value = "Value for TestProp"
.Save
End with
以下將針對郵件項目的每個用戶屬性的名稱和值輸出到即時窗口。
For InxUP = 1 To .UserProperties.Count
Debug.Print " User prop " & InxUP & _
.UserProperties(InxUP).Name & " " & _
.UserProperties(InxUP).Value
Next
下面檢查用戶屬性“TestProp”是否存在,如果存在,則將其值輸出到即時窗口。
Set UserProp = .UserProperties.Find("TestProp")
If Not UserProp Is Nothing Then
Debug.Print " TestProp " & .UserProperties("TestProp").Value
End If
希望這可以幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.