Custom Properties against an Outlook Interop MailItem
I am adding custom properties to a Microsoft.Office.Interop.Outlook.MailIte开发者_JS百科m object like so:
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)
After the four calls above have been executed, I test if I can access the properies using PropertyAccessor.GetProperty, and they are there.
I then save the email to a directory using .SaveAs. When I open the mail item later and try and access the properties I get the following error:
email.PropertyAccessor.GetProperty(SharePointSiteFolder)
The property "http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-C000-000000000046}/SharePointSiteFolder" is unknown or cannot be found.
Can you use ItemProperties
instead?
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
精彩评论