2011-12-18 53 views

回答

1

這個答案已被重寫以下與Outlook實驗

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

感謝。

我的C#足以讓我知道你在做什麼,但我還沒有嘗試從C#訪問Outlook。在Outlook模型中使用和Item。我不知道你可以用你嘗試的方式使用item

如果用戶屬性「test」不存在,UserProperties會引發錯誤。下面我展示如何測試存在。你是否添加一個用戶屬性並忘記保存修改的郵件項目?

以下顯示從Outlook VBA訪問用戶屬性。我期望InterOp模型與語法允許的類似。我知道的重要區別:

  • Set對於C#不是必需的。
  • Nothing是VBA相當於null

變量:

  • FolderItem是已被測試爲類olMail的一個文件夾中的項。
  • UserProp是UserProperty類型。
  • InxUP是整數類型。

下面添加名爲「TestProp」的用戶屬性並鍵入olText,將其值設置爲「TestProp的值」並保存修改的郵件項目。沒有保存,以前的語句不起作用。

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 

希望這有助於

相關問題