gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:34:03 24 4
gpt4 key购买 nike

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

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

我的代码:

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

}
}
}

最佳答案

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

当我不得不使用 Office Interop 时,我通过使用 Late Binding 而不是 Early Binding 来避免这个问题。这意味着我不会添加对特定版本 Office 的引用,并且我的代码将适用于任何最新版本,只要我不使用仅存在于特定版本中的功能或类似功能即可。

本文包含基本教程,向您展示晚期绑定(bind)和早期绑定(bind)之间的区别:Binding for Office automation servers with Visual C# .NET

我还建议查看这篇文章以获取更多背景信息:Using early binding and late binding in Automation

关于c# - Microsoft.Office.Interop.Word 程序集版本高于引用版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6888950/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com