问题
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
回答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 ofnull
.
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
来源:https://stackoverflow.com/questions/8551655/how-to-read-custom-field-value-of-outlook