我正在向Microsoft.Office.Interop.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.
答案 0 :(得分: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