The Microsoft.Office.Interop.Word assembly version

2019-05-03 07:09发布

问题:

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

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

        }
    }
}

回答1:

It seems like your code reference one version of Office, but is trying to use a different version. This is quite a common issue since a lot of different versions of Office are in use.

When I've had to use Office Interop, I avoid this issue by using Late Binding rather than 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.

This article includes basic tutorials to show you the difference between late and early binding: Binding for Office automation servers with Visual C# .NET

I'd also suggest looking at this article for more background information: Using early binding and late binding in Automation



回答2:

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. remember to remove version 14 from References before that. that solved my problem.



回答3:

I had this problem when I upgraded my project from VS2005 to VS2010. I guess Visual Studio automatically upgrades dll's if there's a newer version.

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. 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.

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. Gotta look for it, too.

Good luck everyone.



回答4:

just delete the higher version interop reference and add the version which used working, then it will be fine