我是 C#、.NETFramework 和 VSTO 的新手。我正在尝试通过
EntryID
和 StoreID
通过 GetItemFromID(entryId, storeId)
方法访问 C# 代码中的 Outlook 项目,如下所示:
static public object GetObj(string entryId, string storeId)
{
var app = new Outlook.Application();
var ns = app.Session; // or app.GetNamespace("MAPI");
object x = ns.GetItemFromID(entryId, storeId);
return x;
}
这会导致:
System.Runtime.InteropServices.COMException: 'Retrieving the COM class factory for component with
CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 8000ffff
Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)).'
堆栈跟踪是:
at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
<calls from my other code here, which I removed>
由于这个SO答案,我走上了这条道路,并在互联网上的其他地方找到了相同的方法。我也尝试通过使用
app.GetNamespace("MAPI")
来解决这个问题,但它也没有帮助。
我通过的另一种方法是使用现有的
Namespace
对象,但我找不到任何 API 或有关如何 get
的示例。
感谢任何建议!
还有几个问题我很想听听您的意见:
GetOrCreate
的方法来获取 Namespace
对象?异常原因是只能有一个
Outlook.Application
在运行。此 VSTO 插件适用于 Outlook,因为它已经在运行,因此 GetObj()
无法创建新的互操作实例。
我在寻找 get
当前正在运行的实例时偶然发现了
thisSO 答案。
Outlook.Application app = (Outlook.Application)System.Runtime.InteropServices
.Marshal.GetActiveObject("Outlook.Application");