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

发布于 2024-11-27 04:55:10 字数 3399 浏览 1 评论 0原文

出现以下错误的原因是什么:

错误 12 程序集“Microsoft.Office.Interop.Word,版本=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 使用 'office, 版本=14.0.0.0,文化=中性,PublicKeyToken=71e9bce111e9429c' 它的版本比引用的程序集“office”更高, 版本=12.0.0.0,文化=中立, PublicKeyToken=71e9bce111e9429c' c:\Program Files\Microsoft Visual Studio 10.0\Visual Studio 工具 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);

        }
    }
}

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

        }
    }
}

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

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

发布评论

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

评论(4

手长情犹 2024-12-04 04:55:10

您的代码似乎引用了 Office 的一个版本,但正在尝试使用不同的版本。这是一个相当常见的问题,因为使用了许多不同版本的 Office。

当我必须使用 Office Interop 时,我通过使用Late Binding 而不是Early Binding 来避免此问题。这意味着我不会添加对特定版本 Office 的引用,并且只要我不使用仅存在于某些版本中的函数或类似函数,我的代码就可以与任何最新版本一起使用。

本文包含一些基本教程,旨在向您展示后期绑定和早期绑定之间的区别:使用 Visual C# 绑定 Office 自动化服务器.NET

我还建议查看这篇文章以获取更多背景信息:使用早期绑定和晚的自动化中的绑定

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

又怨 2024-12-04 04:55:10

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

我取消附加 .dll 并将其删除,然后再次添加,但找到了正确的版本(在本例中为 12.0.0.0),问题得到解决。从 Bin 目录中删除应该可以,但如果不行,请在项目中(也许在 web.config 中)搜索 dll 或引用名称并删除,它应该会自行更新。

顺便说一下,一开始我只取消引用了 Office.Interop dll 文件,但一直失败后,我删除了一个名为 Office.dll 的 dll,然后再次添加,它就工作了。也得找找看

祝大家好运。

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.

野稚 2024-12-04 04:55:10

我遇到了同样的问题,从 Add References , .Net 选项卡中,您可以添加 Microsoft.Office.Interop.Word Version=12.0.0.0 而不是 Microsoft.Office.Interop.Word, Version=14.0.0.0。记得在此之前从参考文献中删除版本 14。这解决了我的问题。

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.

美胚控场 2024-12-04 04:55:10

只需删除较高版本的互操作参考并添加使用过的版本即可

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

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