在 Visual Studio C# 中通过代码打开 Word 文档

发布于 2024-11-17 17:20:24 字数 1394 浏览 2 评论 0原文

我正在使用 Visual Studio 进行 Office 开发。并收到以下错误

Error: 
**
Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
**

代码:(也在 https://gist.github.com/1056809

if (File.Exists((string)strDocPath))
{
    Word.Application wdApp = new Word.Application();
    wdApp.Visible = true; //Error thrown here

    object readOnly = false;
    object isVisible = true;
    object oMissing = System.Reflection.Missing.Value;

    //Open the word document
    //Error thrown on line below.
    Word.Document aDoc = wdApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing); 


    // Activate the document
    aDoc.Activate();
}

什么这是错误吗?我怎样才能避免它?

I am developing a Office Development with Visual Studio. And receive the error below

Error: 
**
Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
**

Code: (also at https://gist.github.com/1056809 )

if (File.Exists((string)strDocPath))
{
    Word.Application wdApp = new Word.Application();
    wdApp.Visible = true; //Error thrown here

    object readOnly = false;
    object isVisible = true;
    object oMissing = System.Reflection.Missing.Value;

    //Open the word document
    //Error thrown on line below.
    Word.Document aDoc = wdApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing); 


    // Activate the document
    aDoc.Activate();
}

What is this error? How may I avoid it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲琵琶半遮面シ 2024-11-24 17:20:24

问题是HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID{000209FF-0000-0000-C000-000000000046}无法注册。
尽管该类 (Microsoft.Office.Interop.Word.ApplicationClass) 存在于您的注册表中,但这并不意味着它已被注册。由于某些无法解释的原因,Microsoft 不允许注册 Microsoft.Office.Interop.Word.dll,因此,如果您在代码中引用“ApplicationClass”类;当部署到实际服务器时你会遇到这个问题。您不会在本地/构建计算机上收到错误消息/警告。
一般错误可能如下所示:

检索具有 CLSID 的组件的 COM 类工厂
由于以下原因,{000209FF-0000-0000-C000-000000000046} 失败
错误:80040154 类未注册(HRESULT 异常:
0x80040154(REGDB_E_CLASSNOTREG))。

结论:
即使您已安装/激活并安装了 Microsoft Office 2007/2010获得许可。无法注册“Microsoft.Office.Interop.Word.dll”dll。我花了几乎整整一周的时间才弄清楚这一点。注意:亲自查看;下载-安装-运行“RegDllView”。您可以看到“WINWORD.EXE”的所有已注册 DLL。请注意,不会显示 {000209FF-0000-0000-C000-000000000046}。即使尝试使用该程序手动注册“Microsoft.Office.Interop.Word.dll”也会失败,并显示错误代码 127。我相信解决方法实际上是使用 Microsoft.Office 中提供的“文档”界面。 Interop.Word.dll。归根结底,它只是 WINWORD.EXE 的一个包装。

The problem is HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID{000209FF-0000-0000-C000-000000000046} cannot be registered.
Although the class exists (Microsoft.Office.Interop.Word.ApplicationClass) in your registry, does not mean it has been registered. Microsoft doesn't allow the Microsoft.Office.Interop.Word.dll to be registered for some unexplainable reason and as a result, if you are referencing "ApplicationClass" class in your code; you'll run into this issue when deployed to an actual server. You won't get an error message/warning on your local/build machine.
HERE's what the generic error may look like:

Retrieving the COM class factory for component with CLSID
{000209FF-0000-0000-C000-000000000046} failed due to the following
error: 80040154 Class not registered (Exception from HRESULT:
0x80040154 (REGDB_E_CLASSNOTREG)).

Conclusion:
Even if you have Microsoft Office 2007/2010 installed/activated & licensed. The "Microsoft.Office.Interop.Word.dll" dll cannot be registered. It took me almost an entire week to figure this out. NOTE: To see for yourself; download-install-run "RegDllView". You can see all the registered DLL's for "WINWORD.EXE". Take note, {000209FF-0000-0000-C000-000000000046} will not be displayed. Attempting to even manually registering the "Microsoft.Office.Interop.Word.dll" using the program will fail with an error code 127. I believe the work around is actually using the provided "Document" interface with-in the Microsoft.Office.Interop.Word.dll. It's simply just a wrapper for WINWORD.EXE is what it comes down to.

柠檬心 2024-11-24 17:20:24

尝试将 if 语句后的第一行替换为如下内容:

Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();

然后确保添加对“Microsoft Word 12.0 Object Library”COM 对象的引用,该对象在解决方案资源管理器中看起来像“Microsoft.Office.Interop.Word”。

我对此进行了测试,并出现了一个空白的 MS Word 应用程序。那么让我们看看我们是否能走到这一步。

Try replacing your first line after the if statement with something like this:

Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();

Then make sure you add a reference to "Microsoft Word 12.0 Object Library" COM object, which will look like "Microsoft.Office.Interop.Word"in the Solution explorer.

I tested this and a blank MS Word application came up. So let's see if we can get that far.

开始看清了 2024-11-24 17:20:24

我知道这有点“过期”,但有些人可能仍然难以找到一个简单的解决方案(就像我自己一样),因此我的回复。

根据 @Marine_Elite 帖子,该类未正确注册,但有一个解决方法(我认为)比在 WINWORD.EXE 周围编写自定义包装器容易得多。

我使用了此处< /a> 并将其应用到 MSWORD.OLB

简而言之,您可能只需要下载
regtlibv12 .exe 并针对上述 OLB 运行它,但强烈建议遵循提供的链接中的所有步骤。

我使用的最后一个命令是:
regtlibv12.exe "C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB"

从那时起,我的代码运行得很好,尽管我我不确定应用程序在不同主机上的行为,甚至在重新启动后在我自己的机器上的行为。不过,在后一种情况下可能出现的潜在问题中,再次注册 OLB 是一个简单的解决方案。

I know it's a bit "overdue", but some might still struggle with finding an easy solution (like myself did), hence my reply.

As per @Marine_Elite post, the class is not registered correctly but there is a workaround (I think) much easier than writting a custom wrapper around WINWORD.EXE.

I've used the example taken from here and applied it to the MSWORD.OLB

In short, you'll probably need just to download the regtlibv12.exe and run it against the OLB mentioned above, but following all the steps from the provided link is highly recommended.

The finall command I've used is:
regtlibv12.exe "C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB"

Since then my code runs just fine, although I'm not sure how the application will behave on a different host or even on my own machine after a reboot. Registering the OLB once again in the latter scenario potential problems is a simple solution though.

烟─花易冷 2024-11-24 17:20:24

这段代码对我有用:

        openFileDialog1.InitialDirectory = @"D:\Desktop";
        openFileDialog1.RestoreDirectory = true;
        openFileDialog1.Title = "Browse Text Files";
      
        openFileDialog1.Filter = "MS Words Documents (*.docx,*.doc)|*.docx;*.doc|All files (*.*)|*.*";
        openFileDialog1.FilterIndex =1;
        openFileDialog1.ShowDialog();
        object strDocPath = openFileDialog1.FileName;

        if (File.Exists((string)strDocPath))
        {
            Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
            wdApp.Visible = true; //Error thrown here

            object readOnly = false;
            object isVisible = true;
            object oMissing = System.Reflection.Missing.Value;

        
            Microsoft.Office.Interop.Word.Document aDoc = wdApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing);


          
            aDoc.Activate();
        }


   

this code work for me:

        openFileDialog1.InitialDirectory = @"D:\Desktop";
        openFileDialog1.RestoreDirectory = true;
        openFileDialog1.Title = "Browse Text Files";
      
        openFileDialog1.Filter = "MS Words Documents (*.docx,*.doc)|*.docx;*.doc|All files (*.*)|*.*";
        openFileDialog1.FilterIndex =1;
        openFileDialog1.ShowDialog();
        object strDocPath = openFileDialog1.FileName;

        if (File.Exists((string)strDocPath))
        {
            Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();
            wdApp.Visible = true; //Error thrown here

            object readOnly = false;
            object isVisible = true;
            object oMissing = System.Reflection.Missing.Value;

        
            Microsoft.Office.Interop.Word.Document aDoc = wdApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing,
                                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                                      ref oMissing, ref oMissing, ref oMissing, ref oMissing);


          
            aDoc.Activate();
        }


   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文