简体   繁体   English

无法将“microsoft.Office.Interop.Excel.ApplicationClass”类型的 COM 对象转换为“microsoft.Office.Interop.Excel.Application””

[英]unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'"

This is my first attempt to capture some data from excel from within one C# console application.这是我第一次尝试从一个 C# 控制台应用程序中的 excel 中捕获一些数据。

I get the error "unable to cast COM object of type 'microsoft.Office.Interop.Excel.ApplicationClass' to 'microsoft.Office.Interop.Excel.Application'".我收到错误“无法将'microsoft.Office.Interop.Excel.ApplicationClass'类型的COM对象转换为'microsoft.Office.Interop.Excel.Application'”。

This code used the 'Microsoft Excel 12.0 Object Library' , and I gave reference to Microsoft.Office.Interop.Excel.这段代码使用了“Microsoft Excel 12.0 Object Library”,我参考了 Microsoft.Office.Interop.Excel。

Still, I could not get over this error - which I believe has its own quick solution.尽管如此,我还是无法克服这个错误——我相信它有它自己的快速解决方案。

I digged a little bit this site and came across with this solution: Interop type cannot be embedded我挖掘了一点这个站点并遇到了这个解决方案: 无法嵌入互操作类型

However, I could not understand so could not implement what was suggested as a solution.但是,我无法理解,因此无法实施建议的解决方案。

My .Net version is 4.5.我的 .Net 版本是 4.5。

Any help is very much appreciated.很感谢任何形式的帮助。

Thanks in advance.提前致谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Excel = Microsoft.Office.Interop.Excel;

namespace deneme
{
class Program
{
    static void Main(string[] args)
    {

        Excel.Application xlApp = new Excel.Application();
        xlApp.Visible = true; // <-- excel application
        xlApp.DisplayAlerts = false;

        // Open the workbook.
        Excel.Workbook wBook = xlApp.Workbooks.Open("C:\\FNN\\XLA\\fnnComTemplate.xlsx",
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

        // get the sheet
        Excel.Worksheet wSheet = wBook.Sheets[0];
        // foreach (Excel.Worksheet sheet in wBook.Sheets) { if (sheet.Name == "templateSheet") { wSheet = sheet; } }

        Excel.Range rng = wSheet.get_Range("A1");

        aux = wSheet.Range["F6"].Value;

        Console.WriteLine("interop result:" + aux);
        Console.ReadLine();

    }

}
}

Microsoft Office 365微软办公室 365

I was running Microsoft Office 365 with Windows 10 and tried the mentioned solutions to remove the registry key without success.我在 Windows 10 上运行 Microsoft Office 365,并尝试了上述解决方案来删除注册表项,但没有成功。

I went to the control panel in an attempt to repair the Office 365 suite.我去控制面板试图修复 Office 365 套件。 在此处输入图像描述

  1. Right Click on Start Menu右键单击开始菜单
  2. Click Apps and Features单击应用程序和功能
  3. Search Microsoft 365搜索 Microsoft 365
  4. Click Microsoft 365单击 Microsoft 365
  5. Click Modify点击修改

I selected the quick repair我选择了快修在此处输入图像描述

Tried my program again and everything worked!再次尝试了我的程序,一切正常!

If this is not a code related error than please remove below given key from registry.如果这不是与代码相关的错误,请从注册表中删除以下给定键。

Steps:脚步:

Start--> Run --> regedit --> HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.8/1.7 (Delete this)开始--> 运行--> regedit--> HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046}--> 1.8/1.7(删除这个)

Solution: This issue can be resolved by deleting an invalid registry key left over from the higher version of Office.解决方案:此问题可以通过删除 Office 较高版本遗留的无效注册表项来解决。 Follow the above steps.请按照上述步骤操作。

Cause: This issue is caused by a left over registry key if you have downgraded your version of Microsoft Office from either Office 2010 to Office 2007, or Office 2013 to Office 2010 or 2007.原因:如果您已将 Microsoft Office 版本从 Office 2010 降级到 Office 2007,或将 Office 2013 降级到 Office 2010 或 2007,则此问题是由遗留的注册表项引起的。

Let me know if this solution is not helped to you如果此解决方案对您没有帮助,请告诉我

I am sure the answers to this question (and similar questions) proposing editing the registry and removing old references to versions of Office no longer on the machine are correct .我确信这个问题(和类似问题)建议编辑注册表并删除对不再在机器上的 Office 版本的旧引用的答案是正确的。

I am only adding this answer because I am currently kicking myself.我只是添加这个答案,因为我目前正在踢自己。 Two hours spent hunting keys in the registry and getting nowhere.花了两个小时在注册表中寻找密钥,却一无所获。 Last throw of the dice was to try a simple repair on Microsoft Office and suddenly my error is gone.最后一次掷骰子是尝试在 Microsoft Office 上进行简单的修复,突然我的错误消失了。

... seems to me it's worth trying a repair before getting too deep into manually editing the registry. ...在我看来,在深入手动编辑注册表之前尝试修复是值得的。

Windows 7 x64 / Office 365 (16.0.x) 32-bit / VS 2019 Windows 7 x64 / Office 365 (16.0.x) 32 位 / VS 2019

Was checked Registry ->HKEY_CLASSES_ROOT->TypeLib, look for {00020813-0000-0000-C000-000000000046} exist only one record "1.9", nothing else.检查注册表->HKEY_CLASSES_ROOT->TypeLib,查找{00020813-0000-0000-C000-000000000046}只存在一条记录“1.9”,没有别的。

Checking problem by Process Monitor detecting possible problem with finding and reading from vbe6ext.olb (many results with "NAME NOT FOUND").通过进程监视器检查问题,检测从vbe6ext.olb查找和读取可能存在的问题(许多结果带有“NAME NOT FOUND”)。

Was made "Quick Repair" from Repair Office from the Control Panel , after that problem is gone.问题消失后, 从控制面板的维修办公室进行了“快速修复”。

PS> Was made checking "after treatment" by Process Monitor - now file VBE6EXT.OLB are taking from C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA\VBA6\ by first probe. PS> 由进程监视器检查“处理后” - 现在文件VBE6EXT.OLB正在从C:\Program Files (x86)\Microsoft Office\root\VFS\ProgramFilesCommonX86\Microsoft Shared\VBA\VBA6\通过第一次探测获取。

In my case I delete regedit in:就我而言,我在以下位置删除 regedit:

HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}

delete 1.9删除 1.9 在此处输入图像描述

and problem solved.问题解决了。

A machine had been updated to Office 2016 64-bit and the COM interface was throwing exceptions when called from the a 32-bit application.一台机器已更新到 Office 2016 64 位,当从 32 位应用程序调用时,COM 接口抛出异常。 It did not have old TypeLib entries like the ones in Jayesh's answer.它没有像 Jayesh 的答案那样的旧 TypeLib 条目。

Comparing ProcessMonitor traces to a working machine with Office 2016 led to what look like dead end registry keys in将 ProcessMonitor 跟踪与使用 Office 2016 的工作机器进行比较会导致在

HKEY_CLASSES_ROOT\Wow6432Node\TypeLib\{00024.. HKEY_CLASSES_ROOT\Wow6432Node\TypeLib\{00024..

The working machine would check for the non-existent registry key and return工作机器将检查不存在的注册表项并返回

NAME NOT FOUND未找到姓名

The failing machine had the registry key but then would throw an exception shortly thereafter.故障机器拥有注册表项,但随后不久将引发异常。

After repeatedly deleting the registry key, rerunning the trace, deleting the next failing key, the COM interface functionality was restored.在反复删除注册表项、重新运行跟踪、删除下一个失败的项后,COM 接口功能得以恢复。

Had both 1.7 and 1.8 in registry.在注册表中有 1.7 和 1.8。 Resolved by removing:通过删除解决:

HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.8

Repair Office did not work for me.维修办公室对我不起作用。 I did a full repair but still the problem is there.我进行了全面修复,但问题仍然存在。

This worked for me:这对我有用:

Start--> Run --> regedit --> HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046} --> 1.8/1.7 (Delete the entire node)开始-->运行-->regedit-->HKEY_CLASSES_ROOT-->TypeLib-->{00020813-0000-0000-C000-000000000046}-->1.8/1.7(删除整个节点)

Office will auto repair when you invoke from Visual Studio your Interop App.当您从 Visual Studio 调用 Interop App 时,Office 将自动修复。

My situation is I installed Office 2016 but only a trial version.我的情况是我安装了 Office 2016 但只有试用版。 I removed it and switched back to Office 2013.我将其删除并切换回 Office 2013。

After that, this problem occurred.之后,就出现了这个问题。 The deletion of the key prompted an Auto Repair of Office 2013 that solved my problem.删除密钥提示 Office 2013 的自动修复解决了我的问题。

I had this issue in PowerShell.我在 PowerShell 中遇到了这个问题。 Along with the mentioned issue, $xlApp.Workbooks returned null for me.除了提到的问题,$xlApp.Workbooks 还为我返回了 null。 Running the PowerShell script in 32-bit mode resolved my issue.在 32 位模式下运行 PowerShell 脚本解决了我的问题。

Using the exact same code I got the same problem in a laptop when everything worked fine on another one.使用完全相同的代码,当另一台笔记本电脑一切正常时,我在笔记本电脑上遇到了同样的问题。 I tried various solutions found here and there in the internet but what finally worked, was the explicit definition of the use of non 32 bit version of the library.我尝试了在互联网上到处找到的各种解决方案,但最终奏效的是明确定义使用非 32 位版本的库。 For doing so in visual studio, I had to tick and then untick the box "Prefer 32-bit" in the build section of the project configuration, which added <Prefer32Bit>false</Prefer32Bit> in the .csproj file.为了在 Visual Studio 中这样做,我必须在项目配置的构建部分中勾选并取消勾选“首选 32 位”框,该框在 .csproj 文件中添加了<Prefer32Bit>false</Prefer32Bit>

For whatever reason, my Script just stopped working one day.无论出于何种原因,我的脚本有一天刚刚停止工作。

Running Powershell in 32-bit mode like here circumvented my issue.这里这样在 32 位模式下运行 Powershell 绕过了我的问题。

Nothing worked for me so I removed Office 32-bit and installed the 64-bit version;没有什么对我有用,所以我删除了 Office 32 位并安装了 64 位版本; The Error went away.错误消失了。

My setup: Visual Studio: 2019, Office: 2019 x64, Target Framework: net5.0-windows我的设置: Visual Studio:2019,Office:2019 x64,目标框架:net5.0-windows

Nothing help me from error: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (0x80029C4A (TYPE_E_CANTLOADLIBRARY)).没有什么能帮助我摆脱错误: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (0x80029C4A (TYPE_E_CANTLOADLIBRARY)). Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

So I found via Sysinternals Process Monitor missed file path for Excel.exe at Registry for Win32 and changed it to existing x64 vesion:所以我通过Sysinternals Process Monitor在Win32注册表中找到了Excel.exe的文件路径,并将其更改为现有的x64版本:

[HKEY_CLASSES_ROOT\TypeLib\{00020813-0000-0000-C000-000000000046}\1.9\0\win32]
@="C:\\Program Files\\Microsoft Office\\Root\\Office16\\EXCEL.EXE"

Repairing my Office 2016 did not work and my registry looked fine (1.9 only) but I noticed a couple of the solutions here mentioned 64 and 32 bit versions of office and it made me wonder about how I was compiling my (VB Net) program.修复我的 Office 2016 不起作用,我的注册表看起来很好(仅限 1.9),但我注意到这里的几个解决方案提到了 64 位和 32 位版本的 Office,这让我想知道我是如何编译我的 (VB Net) 程序的。 My Office is 32 bit but my Windows is 64 bit and I was using the "Any CPU" configuration in Visual Studio (2019).我的 Office 是 32 位,但我的 Windows 是 64 位,我在 Visual Studio (2019) 中使用“任何 CPU”配置。 Changing this to the x86 configuration and recompiling solved the problem.将其更改为 x86 配置并重新编译解决了该问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' - C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" Microsoft.Office.Interop.Excel.ApplicationClass没有定义构造函数 - Microsoft.Office.Interop.Excel.ApplicationClass has no constructor defined InvalidCastException-无法转换类型为“ Microsoft.Office.Interop.Outlook.ApplicationClass”的COM对象 - InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' Microsoft.Office.Interop.Excel.Application - 调用不同版本的 Excel - Microsoft.Office.Interop.Excel.Application - Invokeing Different Versions of Excel 将Microsoft.Office.Interop.Excel.Application转换为byte [] - Convert Microsoft.Office.Interop.Excel.Application to byte[] 无法转换“Microsoft.Office.Interop.Excel.WorksheetClass”类型的COM对象 - Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.WorksheetClass' 如何修复“无法转换类型为‘Microsoft.Office.Interop.Outlook.ApplicationClass’的 COM 对象/RPC 服务器不可用”? - How can I fix “Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable”? 如何在asp.net输出流上编写Microsoft.Office.Interop.Excel.Application对象 - How to write Microsoft.Office.Interop.Excel.Application object on asp.net output stream 错误:在为 Microsoft.Office.Interop.Excel.Application 实例化 object 时出现 800700c1 - Error : 800700c1 in instantiating an object for Microsoft.Office.Interop.Excel.Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM