我正在使用Visual Studio 2013(Targeting .NET 4)开发Outlook(2010)加载项。在开发中,某些outlook属性/方法似乎不可用。我认为它与Outlook安全保护有关。
以下代码可以在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
等属性受到保护,但.EntryID
和.Subject
不是。
抛出的错误只是通用的COM异常,它没有给我任何细节:
类型&#39; System.Runtime.InteropServices.COMException&#39;的例外情况发生在MsgSave.dll中但未在用户代码中处理
附加信息:操作中止(HRESULT异常:0x80004004(E_ABORT))
那么,我有没有办法让Outlook“信任”?我的VS代码(并且最终可以信任它),这样可行吗?还是有其他不妥之处?
编辑:谢谢以下两个!简而言之,而不是:Dim oApp As New Outlook.Application
我应该使用:
Dim oApp as Outlook.Application = Globals.ThisAddIn.Application
答案 0 :(得分:2)
不要在Outlook插件中使用New Outlook.Application
- 当您的插件启动时,您可以免费获得Outlook.Application对象。
答案 1 :(得分:1)
某些outlook属性/方法似乎不可用
你在谈论什么属性?你能更具体一点吗?
正如Dmitry建议的那样,您需要使用VSTO提供的Application属性。在这种情况下,您将避免安全提示或例外。通常,当您尝试从独立应用程序自动化Outlook时,会出现此类问题。 VSTO提供的Application对象是受信任的,并且不会为安全属性或方法(例如,MailItem.Send)生成异常。您可以在Outlook "Object Model Guard" Security Issues for Developers文章中详细了解相关内容。