C#调用WPS的COM组件操作

本应比较简单直接调用,Office6下面的文件件etapi.dll (excel) ksoapi.dll (office)  wpsapi.dll(word) wppapi(powerpoint)  互操作类型-》false

结果是管理员启动(Run as时提示Com组件未注册)

   类似 检索 COM 类工厂中 CLSID 为 {000209FF-0000-4B30-A977-D214852036FE} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))。

普通运行正常

原因:一般软件安装时,会提示选择使用用户:当前登陆用户或者所有用户,如果选择当前登陆用户,会将信息写入注册表CurrentUser下;如果选择所有用户,会将信息写入注册表LocalMachine中。

第一步:导出注册表CurrentUse中Software\Classes\Wow6432Node\CLSID内容,修改节点,并导入LocalMachine中Software\Classes\Wow6432Node\CLSID
第二步:导出注册表CurrentUse中Software\Classes\TypeLib内容,修改节点,并导入LocalMachine中Software\Classes\TypeLib

第三步:导出注册表HKEY_CURRENT_USER\SOFTWARE\Classes\Interface 导入到HKEY_LOCAL_MACHINE中的SOFTWARE\Classes\Interface 

 就是将来[HKEY_CURRENT_USER\”全部替换为“[HKEY_LOCAL_MACHINE\”,之后,保存,双击导入注册表 

最后提供懒人专用直接exe双击解决(.Net6运行环境,没钱给360交费,所以报毒,毕竟修改你系统各种注册表,不信者就不必下载了)

直接粘下源码如下

复制代码
 internal class Program
 {
     static void Main(string[] args)
     {
         // 导出并导入 Wow6432Node\CLSID
         CopyRegistryKey(
             Registry.CurrentUser, @"Software\Classes\Wow6432Node\CLSID",
             Registry.LocalMachine, @"Software\Classes\Wow6432Node\CLSID");

         // 导出并导入 TypeLib
         CopyRegistryKey(
             Registry.CurrentUser, @"Software\Classes\TypeLib",
             Registry.LocalMachine, @"Software\Classes\TypeLib");

         // 导出并导入 Interface
         CopyRegistryKey(
             Registry.CurrentUser, @"SOFTWARE\Classes\Interface",
             Registry.LocalMachine, @"SOFTWARE\Classes\Interface");

     }
     static void CopyRegistryKey(RegistryKey sourceRoot, string sourceKey, RegistryKey destRoot, string destKey)
     {
         using (RegistryKey sourceRegistryKey = sourceRoot.OpenSubKey(sourceKey))
         {
             if (sourceRegistryKey == null)
             {
                 Console.WriteLine($"源路径不存在: {sourceRoot.Name}\\{sourceKey}");
                 return;
             }

             using (RegistryKey destRegistryKey = destRoot.CreateSubKey(destKey))
             {
                 if (destRegistryKey == null)
                 {
                     Console.WriteLine($"目标路径无法创建: {destRoot.Name}\\{destKey}");
                     return;
                 }

                 CopyKey(sourceRegistryKey, destRegistryKey);
             }
         }
     }
     static void CopyKey(RegistryKey sourceKey, RegistryKey destKey)
     {
         // 复制值
         foreach (string valueName in sourceKey.GetValueNames())
         {
             object value = sourceKey.GetValue(valueName);
             RegistryValueKind valueKind = sourceKey.GetValueKind(valueName);
             destKey.SetValue(valueName, value, valueKind);
         }

         // 复制子键
         foreach (string subKeyName in sourceKey.GetSubKeyNames())
         {
             using (RegistryKey sourceSubKey = sourceKey.OpenSubKey(subKeyName))
             using (RegistryKey destSubKey = destKey.CreateSubKey(subKeyName))
             {
                 CopyKey(sourceSubKey, destSubKey);
             }
         }
     }
 }
复制代码

 {000209FF-0000-0000-C000-000000000046}  word

{00024500-0000-0000-C000-000000000046}  excel

{91493441-5A91-11CF-8700-00AA0060263B}  ppt

posted @   stweily  阅读(1360)  评论(2编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示