简体   繁体   English

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

[英]The Microsoft.Office.Interop.Word assembly version is higher than referenced

What is the cause of the following error: 导致以下错误的原因是什么:

Error 12 Assembly 'Microsoft.Office.Interop.Word, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' uses 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' which has a higher version than referenced assembly 'office, Version=12.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 错误12程序集'Microsoft.Office.Interop.Word,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c'使用'office,Version = 14.0.0.0,Culture = neutral,PublicKeyToken = 71e9bce111e9429c',其版本高于引用程序集'office,版本= 12.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

my code : 我的代码:

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);

        }
    }
}

It seems like your code reference one version of Office, but is trying to use a different version. 您的代码似乎引用了Office的一个版本,但尝试使用其他版本。 This is quite a common issue since a lot of different versions of Office are in use. 这是一个非常常见的问题,因为许多不同版本的Office正在使用中。

When I've had to use Office Interop, I avoid this issue by using Late Binding rather than Early Binding . 当我不得不使用Office Interop时,我通过使用Late Binding而不是Early Binding来避免这个问题。 This means that I don't add a reference to a specific version of Office and my code will work with any recent version as long as I'm not using a function or similar that only exist in certain versions. 这意味着我不添加对特定版本的Office的引用,只要我没有使用仅存在于某些版本中的函数或类似函数,我的代码将适用于任何最新版本。

This article includes basic tutorials to show you the difference between late and early binding: Binding for Office automation servers with Visual C# .NET 本文包括基本教程,向您展示后期绑定和早期绑定之间的区别: 使用Visual C#.NET绑定Office自动化服务器

I'd also suggest looking at this article for more background information: Using early binding and late binding in Automation 我还建议查看本文以获取更多背景信息: 在Automation中使用早期绑定和后期绑定

I had this problem when I upgraded my project from VS2005 to VS2010. 当我将项目从VS2005升级到VS2010时,我遇到了这个问题。 I guess Visual Studio automatically upgrades dll's if there's a newer version. 我想如果有更新的版本,Visual Studio会自动升级dll。

I unattached the .dll's and deleted, then added again but finding the correct version (in this case 12.0.0.0 ) and the problem was solved. 我取消了.dll并删除了,然后再次添加但找到了正确的版本(在本例中为12.0.0.0 )并且问题解决了。 Deleting from Bin directory should work but if not, search dll or reference name in the project, maybe in web.config and delete, it should update itself. 从Bin目录中删除应该可以工作,但如果没有,在项目中搜索dll或引用名称,可能在web.config和delete中,它应该自行更新。

By the way, at first I only unreferenced Office.Interop dll files but after it kept failing, I deleted a dll named office.dll and added again, it worked. 顺便说一下,起初我只是没有引用Office.Interop dll文件,但在它保持失败之后,我删除了一个名为office.dll的dll并再次添加,它有效。 Gotta look for it, too. 也要找它。

Good luck everyone. 祝大家好运。

I had the same problem, from Add References , .Net tab you can add Microsoft.Office.Interop.Word Version=12.0.0.0 instead of Microsoft.Office.Interop.Word, Version=14.0.0.0. 我有同样的问题,从添加引用,.Net选项卡,您可以添加Microsoft.Office.Interop.Word版本= 12.0.0.0而不是Microsoft.Office.Interop.Word,版本= 14.0.0.0。 remember to remove version 14 from References before that. 记得在此之前从References中删除版本14。 that solved my problem. 这解决了我的问题。

只是删除更高版本的互操作引用并添加使用工作的版本,然后就可以了

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM