造成下列错误的原因是什么?
错误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,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c‘c:\Program \MicrosoftVisualStudio10.0\ Visual 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);
}
}
}
发布于 2011-08-08 14:15:44
您的代码似乎引用了Office的一个版本,但正在尝试使用不同的版本。这是一个很常见的问题,因为很多不同版本的Office都在使用。
当我不得不使用Office时,我通过使用Late Binding
而不是Early Binding
来避免这个问题。这意味着我不添加对Office特定版本的引用,只要我不使用仅存在于某些版本中的函数或类似的函数,我的代码就会与任何最近的版本一起工作。
本文包括一些基本教程,向您展示延迟绑定和早期绑定之间的区别:用VisualC# .NET绑定办公自动化服务器
我还建议查看本文以获得更多的背景信息:自动化中使用早期绑定和后期绑定
发布于 2012-05-03 13:21:28
当我将我的项目从VS2005升级到VS2010时,我遇到了这个问题。如果有较新的版本,Visual会自动升级dll。
我解除了..dll的连接并删除了它,然后再次添加,但是找到了正确的版本(在本例中是12.0.0.0
),问题就解决了。删除Bin目录应该有效,但如果不能,则在项目中搜索dll或引用名称,可能在web.config
和delete中,它应该更新自己。
顺便说一下,一开始我只引用了未引用的Office.Interop
dll文件,但是在它继续失败之后,我删除了一个名为office.dll
的dll,并再次添加了它。我也得去找找。
祝大家好运。
发布于 2011-08-09 10:03:15
我也遇到了同样的问题,从Add,.Net选项卡,您可以添加Microsoft.Office.Interop.Word Version=12.0.0.0而不是Microsoft.Office.Interop.Word,Version=14.0.0.0。记得在此之前从引用中删除版本14。解决了我的问题。
https://stackoverflow.com/questions/6888950
复制相似问题