VSTO 库没有注册。 (异常来自 HRESULT:0x8002801D (TYPE_E_LIBNOTREGISTERED))

majormayer
  • 966

我需要开发2个VSTO项目程序:

  1. VSTO for PPT
  2. VSTO for Word

在PPT程序中,唤醒/打开 Word 程序,并执行 Word 中 VSTO 代码.
代码如下:

PPT中唤醒/打开 Word:

//获取word程序
Word.Application wordApp;
try { wordApp = Marshal.GetActiveObject("Word.Application") as Word.Application; }
catch { wordApp = new Word.Application(); }

wordApp.Visible = true;
wordApp.Documents.Add();
wordApp.Activate();

COMAddIn comaddin = wordApp.COMAddIns.Item("BaxEduWord");
System.Windows.MessageBox.Show(comaddin.Object.test("rrrrr"));

Word 中暴露 VSTO 对象,让 PPT程序中调用:
创建 AddInUtilities.cs

using System.Runtime.InteropServices;

namespace BaxEduWord
{
    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IAddInUtilities
    {
        string test(string inp);
    }

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    public class AddInUtilities : StandardOleMarshalObject, IAddInUtilities
    {
        public string test(string inp)
        {
            return inp;
        }
    }
}

ThisAddIn中重写RequestComAddInAutomationService暴露对象:

/// <summary>
/// 暴露当前VSTO模型
/// </summary>
private AddInUtilities utilities;
protected override object RequestComAddInAutomationService()
{
    if (utilities == null)
        utilities = new AddInUtilities();

    return utilities;
}

然后我运行 PPT 程序,出现错误:
ppt成功的将Word唤醒,并且得到wordapplication,
但从application中希望得到word的外界程序COMAddIns时出现异常:

clipboard.png

我尝试了下面办法:
对比一下注册表版本信息:
1,计算机HKEY_CLASSES_ROOTWOW6432NodeInterface{00020970-0000-0000-C000-000000000046}TypeLib

clipboard.png

2,HKEY_CLASSES_ROOTWOW6432NodeTypeLib{00020905-0000-0000-C000-000000000046}
clipboard.png

电脑曾经安装过 WPS

上面代码原封不动 在同事电脑上可以正常运行 !!!

回复
阅读 4k
1 个回答

使用下面封装的互操作类:

using Common.Logging;

using Infrastructure.Hook;

using Microsoft.Office.Core;

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;

using VarEnum;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;

namespace Application.ComAddin
{
    /// <summary>
    /// 加载项互操作扩展
    /// </summary>
    public static class ComAddinEx
    {
        public static readonly ILog _log = LogManager.GetLogger("ComAddinEx");

        //获取程序错误次数
        static int _getApplicationErrorCount = 0;

        /// <summary>
        /// 唤醒Word程序
        /// </summary>
        public static Word.Application OpenWordApp()
        {
            //获取word程序
            Word.Application wordApp = null;
            try
            {
                wordApp = GetWordApplication(out _);
                //即便不出错也可能得到Null
                if (wordApp == null) throw new Exception("未找到运行中的Word程序,需要新开Word");
            }
            catch (Exception err)
            {
                _log.Error(err);
                wordApp = new Word.Application();
            }

            if (wordApp != null)
            {
                try
                {
                    Thread.Sleep(500);
                    wordApp.Visible = true;
                    wordApp.Activate();
                }
                finally
                {
                    IntPtr Hwnd = AddinHandle.GetWordHandle();
                    _ = Hook.ShowWindow(Hwnd, 3);
                }
            }

            return wordApp;
        }


        /// <summary>
        /// 从运行程序中唤醒PPT程序
        /// </summary>
        public static PowerPoint.Application GetPptApp()
        {
            //获取PPT程序
            PowerPoint.Application pptApp;

            try { pptApp = GetPptApplication(out _); }
            catch (Exception err)
            {
                _log.Error(err);
                pptApp = null;
            }

            if (pptApp != null)
            {
                try
                {
                    Thread.Sleep(500);
                    pptApp.Visible = MsoTriState.msoTrue;
                    pptApp.Activate();
                }
                finally
                {
                    _ = Hook.ShowWindow((IntPtr)pptApp.HWND, 3);
                }
            }

            return pptApp;
        }

        /// <summary>
        /// 获取baxeduppt加载项
        /// </summary>
        /// <returns></returns>
        public static dynamic GetBaxEduPpt()
        {
            PowerPoint.Application pptApp = GetPptApp();

            COMAddIn comaddin = pptApp.COMAddIns.Item("baxeduppt");
            return comaddin.Object;
        }

        /// <summary>
        /// 获取BaxEduWord加载项
        /// </summary>
        /// <returns></returns>
        public static dynamic GetBaxEduWord()
        {
            Word.Application wordApp = OpenWordApp();

            COMAddIn comaddin = wordApp.COMAddIns.Item("BaxEduWord");
            return comaddin.Object;
        }

        /// <summary>
        /// 释放其他操作加载项程序
        /// </summary>
        public static void Dispose()
        {
            Word.Application wordApp = OpenWordApp();

            if (wordApp != null)
            {
                wordApp.Quit();
                Marshal.FinalReleaseComObject(wordApp);
                wordApp = null;

                GC.Collect();
            }
        }

        /// <summary>
        /// 获取当前处于活动状态的PPT进程
        /// </summary>
        /// <param name="officeType">输出得到的office类型</param>
        /// <returns></returns>
        public static PowerPoint.Application GetPptApplication(out OfficeType officeType)
        {
            officeType = OfficeType.Office;
            try
            {
                PowerPoint.Application pptApp = null;
                Thread.Sleep(100);
                //MS Office
                if (Process.GetProcessesByName("POWERPNT").ToList().Count > 0)
                {
                    pptApp = (PowerPoint.Application)Marshal.GetActiveObject("Powerpoint.Application");
                    _getApplicationErrorCount = 0;
                }
                else if (Process.GetProcessesByName("wpp").ToList().Count > 0)
                {
                    officeType = OfficeType.WPS;
                    //WPS Office V9
                    if (Type.GetTypeFromProgID("KWPP.Application") != null)
                    {
                        pptApp = (PowerPoint.Application)Marshal.GetActiveObject("KWPP.Application");
                        _getApplicationErrorCount = 0;
                    }
                    //WPS Office V8
                    else if (Type.GetTypeFromProgID("WPP.Application") != null)
                    {
                        pptApp = (PowerPoint.Application)Marshal.GetActiveObject("WPP.Application");
                        _getApplicationErrorCount = 0;
                    }
                    else
                        _log.Warn("GetPptApplication:当前WPS 不支持VSTO");
                }
                else
                {
                    pptApp = null;
                }
                return pptApp;
            }
            catch (Exception ex)
            {
                _getApplicationErrorCount++;
                //重试2次
                if (_getApplicationErrorCount <= 2)
                {
                    Thread.Sleep(200);
                    return GetPptApplication(out _);
                }
                else
                {
                    _getApplicationErrorCount = 0;
                    //获取word失败
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 获取当前处于活动状态的Word进程
        /// </summary>
        /// <param name="officeType">输出得到的office类型</param>
        /// <returns></returns>
        public static Word.Application GetWordApplication(out OfficeType officeType)
        {
            officeType = OfficeType.Office;
            try
            {
                Word.Application wdApp = null;
                Thread.Sleep(100);
                //MS Office
                if (Process.GetProcessesByName("WINWORD").ToList().Count > 0)
                {
                    wdApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
                    _getApplicationErrorCount = 0;
                }
                else if (Process.GetProcessesByName("wps").ToList().Count > 0)
                {
                    officeType = OfficeType.WPS;
                    //WPS Office V9
                    if (Type.GetTypeFromProgID("KWPS.Application") != null)
                    {
                        wdApp = (Word.Application)Marshal.GetActiveObject("KWPS.Application");
                        _getApplicationErrorCount = 0;
                    }
                    //WPS Office V8
                    else if (Type.GetTypeFromProgID("WPS.Application") != null)
                    {
                        wdApp = (Word.Application)Marshal.GetActiveObject("WPS.Application");
                        _getApplicationErrorCount = 0;
                    }
                    else
                        _log.Warn("GetWordApplication:当前WPS 不支持VSTO");
                }
                else
                {
                    wdApp = null;
                }
                return wdApp;
            }
            catch (Exception ex)
            {
                _getApplicationErrorCount++;
                //重试2次
                if (_getApplicationErrorCount <= 2)
                {
                    Thread.Sleep(200);
                    return GetWordApplication(out _);
                }
                else
                {
                    _getApplicationErrorCount = 0;
                    //获取word失败
                    _log.Error("第" + _getApplicationErrorCount + "次:" + ex);
                    throw ex;
                }
            }
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏