如何在 Visual Studio Outlook 加载项中访问某些邮件项 properties/methods?
How to access certain mailitem properties/methods in a Visual Studio Outlook Add-In?
我正在使用 Visual Studio 2013(目标 .NET 4)开发 Outlook (2010) 加载项。
某些 Outlook properties/methods 似乎不可用。
以下代码适用于 Outlook VBA。
Public Sub OutlookTest()
'Dim oApp As New Outlook.Application (NOT NEEDED FOR OUTLOOK VBA)
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection object for getting the selection.
Dim oItem As Object ' You don't know the type yet.
Set oExp = Application.ActiveExplorer 'Get the ActiveExplorer.
Set oSel = oExp.Selection ' Get the selection.
For i = 1 To oSel.Count ' Loop through all the currently .selected items
Set oItem = oSel.Item(i) ' Get a selected item.
Call DisplayInfo(oItem) ' Display information about it.
Next i
End Sub
Private Sub DisplayInfo(oItem As Object)
Dim strMessageClass As String
Dim oMailItem As Outlook.MailItem
' You need the message class to determine the type.
strMessageClass = oItem.MessageClass
If (strMessageClass = "IPM.Note") Then ' Mail Entry.
Set oMailItem = oItem
MsgBox oMailItem.Subject
MsgBox oMailItem.EntryID
MsgBox oMailItem.HTMLBody
oMailItem.SaveAs "C:\Users\u001tb7\Desktop\New folder\testOL.msg", olMSG
Else
MsgBox "Pick something else"
End If
End Sub
当我在加载项中尝试与 Visual Studio 几乎相同的代码时:
Private Sub butSettings_Click(sender As Object, e As RibbonControlEventArgs) Handles butSettings.Click
Dim oApp As New Outlook.Application
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection object for getting the selection.
Dim oItem As Object ' You don't know the type yet.
oExp = oApp.ActiveExplorer ' Get the ActiveExplorer.
oSel = oExp.Selection ' Get the selection.
For i = 1 To oSel.Count ' Loop through all the currently .selected items
oItem = oSel.Item(i) ' Get a selected item.
DisplayInfo(oItem) ' Display information about it.
Next i
End Sub
Sub DisplayInfo(oItem As Object)
Dim strMessageClass As String
Dim oMailItem As Outlook.MailItem
' You need the message class to determine the type.
strMessageClass = oItem.MessageClass
If (strMessageClass = "IPM.Note") Then ' Mail Entry.
oMailItem = oItem
MsgBox(oMailItem.Subject)
MsgBox(oMailItem.EntryID)
MsgBox(oMailItem.HTMLBody) '<---FAILS
oMailItem.SaveAs("C:\Users\u001tb7\Desktop\New folder\testVS.msg", Outlook.OlSaveAsType.olMSG) '<---ALSO FAILS
Else
MsgBox("Pick something else")
End If
End Sub
我在 MailItem.HTMLBody
和 MailItem.SaveAs
上收到错误,但在 MailItem.Subject
或 .EntryID
上没有收到错误。
这让我怀疑这与安全有关,因为我认为像 .HTMLBody
这样的属性是 'protected',但 .EntryID
和 .Subject
不是。
错误是通用 COM 异常,没有提供任何详细信息:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in MsgSave.dll but was not handled in user code
Additional information: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
有什么方法可以让 Outlook 'trust' 我的 VS 代码(用于最终分发)?还是有什么不妥?
编辑:感谢以下两位!
而不是:
Dim oApp As New Outlook.Application
使用:
Dim oApp as Outlook.Application = Globals.ThisAddIn.Application
不要在 Outlook 加载项中使用 New Outlook.Application
- 当您的加载项启动时,您会免费获得 Outlook.Application 对象。
certain outlook properties/methods seem to be unavailable
你说的是什么属性?能具体点吗?
正如 Dmitry 所建议的,您需要使用 VSTO 提供的应用程序 属性。在这种情况下,您将避免出现安全提示或异常。通常,当您尝试从独立应用程序自动化 Outlook 时,您会遇到此类问题。 VSTO 提供的应用程序对象是受信任的,不会为安全属性或方法(例如,MailItem.Send)生成异常。您可以在 Outlook "Object Model Guard" Security Issues for Developers 文章中阅读更多相关信息。
我正在使用 Visual Studio 2013(目标 .NET 4)开发 Outlook (2010) 加载项。
某些 Outlook properties/methods 似乎不可用。
以下代码适用于 Outlook VBA。
Public Sub OutlookTest()
'Dim oApp As New Outlook.Application (NOT NEEDED FOR OUTLOOK VBA)
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection object for getting the selection.
Dim oItem As Object ' You don't know the type yet.
Set oExp = Application.ActiveExplorer 'Get the ActiveExplorer.
Set oSel = oExp.Selection ' Get the selection.
For i = 1 To oSel.Count ' Loop through all the currently .selected items
Set oItem = oSel.Item(i) ' Get a selected item.
Call DisplayInfo(oItem) ' Display information about it.
Next i
End Sub
Private Sub DisplayInfo(oItem As Object)
Dim strMessageClass As String
Dim oMailItem As Outlook.MailItem
' You need the message class to determine the type.
strMessageClass = oItem.MessageClass
If (strMessageClass = "IPM.Note") Then ' Mail Entry.
Set oMailItem = oItem
MsgBox oMailItem.Subject
MsgBox oMailItem.EntryID
MsgBox oMailItem.HTMLBody
oMailItem.SaveAs "C:\Users\u001tb7\Desktop\New folder\testOL.msg", olMSG
Else
MsgBox "Pick something else"
End If
End Sub
当我在加载项中尝试与 Visual Studio 几乎相同的代码时:
Private Sub butSettings_Click(sender As Object, e As RibbonControlEventArgs) Handles butSettings.Click
Dim oApp As New Outlook.Application
Dim oExp As Outlook.Explorer
Dim oSel As Outlook.Selection ' You need a selection object for getting the selection.
Dim oItem As Object ' You don't know the type yet.
oExp = oApp.ActiveExplorer ' Get the ActiveExplorer.
oSel = oExp.Selection ' Get the selection.
For i = 1 To oSel.Count ' Loop through all the currently .selected items
oItem = oSel.Item(i) ' Get a selected item.
DisplayInfo(oItem) ' Display information about it.
Next i
End Sub
Sub DisplayInfo(oItem As Object)
Dim strMessageClass As String
Dim oMailItem As Outlook.MailItem
' You need the message class to determine the type.
strMessageClass = oItem.MessageClass
If (strMessageClass = "IPM.Note") Then ' Mail Entry.
oMailItem = oItem
MsgBox(oMailItem.Subject)
MsgBox(oMailItem.EntryID)
MsgBox(oMailItem.HTMLBody) '<---FAILS
oMailItem.SaveAs("C:\Users\u001tb7\Desktop\New folder\testVS.msg", Outlook.OlSaveAsType.olMSG) '<---ALSO FAILS
Else
MsgBox("Pick something else")
End If
End Sub
我在 MailItem.HTMLBody
和 MailItem.SaveAs
上收到错误,但在 MailItem.Subject
或 .EntryID
上没有收到错误。
这让我怀疑这与安全有关,因为我认为像 .HTMLBody
这样的属性是 'protected',但 .EntryID
和 .Subject
不是。
错误是通用 COM 异常,没有提供任何详细信息:
An exception of type 'System.Runtime.InteropServices.COMException' occurred in MsgSave.dll but was not handled in user code
Additional information: Operation aborted (Exception from HRESULT: 0x80004004 (E_ABORT))
有什么方法可以让 Outlook 'trust' 我的 VS 代码(用于最终分发)?还是有什么不妥?
编辑:感谢以下两位!
而不是:
Dim oApp As New Outlook.Application
使用:
Dim oApp as Outlook.Application = Globals.ThisAddIn.Application
不要在 Outlook 加载项中使用 New Outlook.Application
- 当您的加载项启动时,您会免费获得 Outlook.Application 对象。
certain outlook properties/methods seem to be unavailable
你说的是什么属性?能具体点吗?
正如 Dmitry 所建议的,您需要使用 VSTO 提供的应用程序 属性。在这种情况下,您将避免出现安全提示或异常。通常,当您尝试从独立应用程序自动化 Outlook 时,您会遇到此类问题。 VSTO 提供的应用程序对象是受信任的,不会为安全属性或方法(例如,MailItem.Send)生成异常。您可以在 Outlook "Object Model Guard" Security Issues for Developers 文章中阅读更多相关信息。