2011-03-08 69 views
0

我添加自定义属性到Microsoft.Office.Interop.Outlook.MailItem对象,像这样的定制属性:针对一个Outlook互操作的MailItem

Public Const SharePointSiteUrl As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUrl" 
Public Const SharePointSiteFolder As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder" 
Public Const SharePointSiteUsername As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteUsername" 
Public Const SharePointSitePassword As String = "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSitePassword" 

...

email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUrl, sharepointSite) 
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteFolder, sharepointFolder) 
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSiteUsername, sharepointUserName) 
email.PropertyAccessor.SetProperty(OutlookHelper.SharePointSitePassword, sharepointPassword) 

四后上面的调用已经被执行,我测试我是否可以使用PropertyAccessor.GetProperty访问属性,并且它们在那里。

然后我使用.SaveAs将电子邮件保存到一个目录。当我后来打开邮件项目和尝试,并访问我收到以下错误性质:

email.PropertyAccessor.GetProperty(SharePointSiteFolder) 

The property "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder" is unknown or cannot be found. 

回答

2

你可以使用ItemProperties呢?

Option Explicit On 
Option Strict On 

Imports Microsoft.Office.Interop.Outlook 

Public Class Form1 

    Private Shared ReadOnly TestFile As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "Test.msg") 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     SetProperties() 
     GetProperties() 
     Me.Close() 
    End Sub 
    Private Shared Sub GetProperties() 
     Dim app = New Microsoft.Office.Interop.Outlook.Application() 
     Dim email = DirectCast(app.CreateItemFromTemplate(TestFile), Microsoft.Office.Interop.Outlook.MailItem) 
     ''//Write out the properties 
     Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUrl)) 
     Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteFolder)) 
     Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSiteUsername)) 
     Trace.WriteLine(OutlookHelper.GetStringProperty(email, OutlookHelper.SharePointSitePassword)) 
    End Sub 
    Private Shared Sub SetProperties() 
     Dim app As New Microsoft.Office.Interop.Outlook.Application() 
     Dim email = DirectCast(app.CreateItem(OlItemType.olMailItem), MailItem) 

     ''//The values that we want to set 
     Dim sharepointSite = "it was the best of times" 
     Dim sharepointFolder = "it was the worst of times" 
     Dim sharepointUserName = "it was the age of wisdom" 
     Dim sharepointPassword = "it was the age of foolishness" 

     ''//Set the properties 
     OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUrl, sharepointSite) 
     OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteFolder, sharepointFolder) 
     OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSiteUsername, sharepointUserName) 
     OutlookHelper.SetStringProperty(email, OutlookHelper.SharePointSitePassword, sharepointPassword) 

     ''//Save the file 
     email.SaveAs(TestFile) 
    End Sub 

End Class 
Public Class OutlookHelper 
    Public Const SharePointSiteUrl As String = "SharePointSiteUrl" 
    Public Const SharePointSiteFolder As String = "SharePointSiteFolder" 
    Public Const SharePointSiteUsername As String = "SharePointSiteUsername" 
    Public Const SharePointSitePassword As String = "SharePointSitePassword" 

    Public Shared Function GetStringProperty(ByVal mailItem As MailItem, ByVal key As String) As String 
     ''//Validate input 
     If mailItem Is Nothing Then Throw New ArgumentNullException("mailItem") 
     If String.IsNullOrEmpty(key) Then Throw New ArgumentNullException("key") 
     ''//Try to get the property 
     Dim O = mailItem.ItemProperties(key) 
     ''//See if we got something and that if it has a value 
     If (O IsNot Nothing) AndAlso (O.Value IsNot Nothing) Then 
      ''//Return the value as a string 
      Return O.Value.ToString() 
     Else 
      ''//Property wasn't found or it was empty 
      Return Nothing 
     End If 
    End Function 
    Public Shared Sub SetStringProperty(ByVal mailItem As MailItem, ByVal key As String, ByVal value As String) 
     ''//Check if the property already exists 
     Dim O = mailItem.ItemProperties(key) 
     ''//If not 
     If (O Is Nothing) Then 
      ''//Create it 
      mailItem.ItemProperties.Add(key, OlUserPropertyType.olText) 
     End If 
     ''//Set the property 
     mailItem.ItemProperties(key).Value = value 
    End Sub 
End Class 
+0

我没看到那个属性。明天我会放弃它。我没有看到为什么没有理由(除非它在Office 2007 COM中不可用)... – 2011-03-08 19:10:28

+0

工作是一种享受。谢谢一堆。 – 2011-03-09 09:29:19