无法获取或创建MAPI命名空间;由“正在检索具有 CLSID 的组件的 COM 类工厂...”异常导致

问题描述 投票:0回答:1

我是 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 配置文件,其中一组为默认配置文件,并且在启动时没有提示选择配置文件。这可能是一个因素吗?
c# vsto outlook-addin .net-4.8
1个回答
0
投票

异常原因是只能有一个

Outlook.Application
在运行。此 VSTO 插件适用于 Outlook,因为它已经在运行,因此
GetObj()
无法创建新的互操作实例。

我在寻找 get当前正在运行的实例时偶然发现了

this
SO 答案。

Outlook.Application app = (Outlook.Application)System.Runtime.InteropServices
.Marshal.GetActiveObject("Outlook.Application");
© www.soinside.com 2019 - 2024. All rights reserved.