Microsoft.Office.Interop.Word程序集的版本高于引用的版本。

5
以下是错误的原因:
错误 12 组件'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'使用了版本高于被引用组件'office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'的'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'。该错误位于以下路径下: c:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Word.dll WindowsFormsApplication1
这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using Application = Microsoft.Office.Interop.Word.Application;
using DataTable = System.Data.DataTable;
using Document = Microsoft.Office.Interop.Word.Document;
using Microsoft.Office;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var wordApp = new Application { Visible = false };
            object objMissing = Missing.Value;
            Document wordDoc = wordApp.Documents.Add(ref objMissing, ref objMissing, ref objMissing, ref objMissing);

            wordApp.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
            wordApp.Selection.TypeParagraph();
            String docNumber = "1";
            String revisionNumber = "0";
            wordApp.Selection.Paragraphs.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
            wordApp.ActiveWindow.Selection.Font.Name = "Arial";
            wordApp.ActiveWindow.Selection.Font.Size = 8;
            wordApp.ActiveWindow.Selection.TypeText("Document #: " + docNumber + " - Revision #: " + revisionNumber);
            wordApp.ActiveWindow.Selection.TypeText("\t");
            wordApp.ActiveWindow.Selection.TypeText("\t");
            wordApp.ActiveWindow.Selection.TypeText("Page ");
            Object CurrentPage = WdFieldType.wdFieldPage;
            wordApp.ActiveWindow.Selection.Fields.Add(wordApp.Selection.Range, ref CurrentPage, ref objMissing, ref objMissing);
            wordApp.ActiveWindow.Selection.TypeText(" of ");
            Object TotalPages = WdFieldType.wdFieldNumPages;
            wordApp.ActiveWindow.Selection.Fields.Add(wordApp.Selection.Range, ref TotalPages, ref objMissing, ref objMissing);
            wordApp.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;

            object c = "d:\\1.doc";
            wordDoc.Paragraphs.LineSpacing = 8;

            Paragraph wp = wordDoc.Paragraphs.Add(ref objMissing);
            wp.Range.Text += richTextBox1.Text;

            wordDoc.SaveAs(ref c, ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
               ref objMissing
               , ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,
               ref objMissing, ref objMissing
               , ref objMissing, ref objMissing);
            (wordDoc).Close(ref objMissing, ref objMissing, ref objMissing);
            (wordApp).Quit(ref objMissing, ref objMissing, ref objMissing);

        }
    }
}
4个回答

6
似乎你的代码引用了一个Office版本,但是尝试使用不同的版本。这是一个很常见的问题,因为有许多不同版本的Office在使用。
当我必须使用Office Interop时,我通过使用“后期绑定”而不是“早期绑定”来避免这个问题。这意味着我不会添加对特定版本Office的引用,只要我没有使用某些仅存在于某些版本中的函数或类似功能,我的代码就可以与任何最近的版本一起工作。
本文包括基本教程,展示后期绑定和早期绑定之间的区别: 使用Visual C# .NET的Office自动化服务器绑定 我还建议阅读此文章以获取更多背景信息:在自动化中使用早期绑定和后期绑定

+1 有一个问题:在我的 Windows 7 (x86) 系统上,我看到一个名为 C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14 的文件夹,其中包含几个与 Office 应用程序(Word、Excel 等)对应的 DLL。这些是随 Visual Studio 还是随 Office 一起安装的?我的问题是:即使没有安装 Office,这些 DLL 也代表着针对 Office 进行编程的能力,因此它们代表了相当重要的功能。谢谢。 - Sabuncu
1
据我所知,现在的Visual Studio Pro及更高版本都带有Office工具,路径似乎表明它们是随VS一起提供的。但要小心,不要遇到与原问题相同的问题(我认为如果您升级到新版本的Visual Studio,可能会带有不同版本的DLL等)。 - Hans Olsson

3

在将我的项目从VS2005升级到VS2010时,我遇到了这个问题。我猜想如果有更新版本,Visual Studio会自动升级dll文件。

我取消了.dll文件的附加并将其删除,然后再次添加正确版本(在这种情况下是12.0.0.0),问题解决了。应该可以通过从Bin目录中删除来解决问题,但如果不行,搜索项目中的dll或引用名称,也许在web.config中,然后删除它,它应该会自动更新。

顺便说一下,起初我只取消了对Office.Interop dll文件的引用,但之后它继续失败,我删掉了一个名为office.dll的dll文件并重新添加,这样就可以了。也得找找这个dll文件。

祝大家好运。


2

我曾经遇到过同样的问题,从“添加引用”中,.Net选项卡中你可以添加Microsoft.Office.Interop.Word Version=12.0.0.0,而不是Microsoft.Office.Interop.Word, Version=14.0.0.0。在此之前请记得移除版本14的引用。这解决了我的问题。


-1

只需删除较高版本的Interop引用并添加正在使用的版本,问题就会解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接