类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”

    技术2022-05-20  45

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Word; namespace SqlTest { public partial class Form1 : Form { object fileName; Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document myWordDoc; string strContent = ""; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { createWord(); } private void createWord() { fileName = System.Windows.Forms.Application.StartupPath + "//test.doc"; if (System.IO.File.Exists((string)fileName)) System.IO.File.Delete((string)fileName); //Nothing只是为了传参数而填入的无效数据. Object Nothing = System.Reflection.Missing.Value; myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //在此可将数据库中的数据读入Wrod中。 strContent = "请问你老家是哪里的?/n我不告诉你!"; myWordDoc.Paragraphs.Last.Range.Text = strContent; //将myWordDoc的内容保存为DOC文档 myWordDoc.SaveAs(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭myWordApp组件对象 myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing); this.richTextBox1.Text = fileName + "/r/n" + "创建成功"; } } }

    提示如下错误:

     

    错误 1 类型“Microsoft.Office.Interop.Word.ApplicationClass”未定义构造函数 E:/Workspaces/C#/SqlTest/SqlTest/Form1.cs 18 63 SqlTest

     

    错误 2 无法嵌入互操作类型“Microsoft.Office.Interop.Word.ApplicationClass”。请改用适用的接口。 E:/Workspaces/C#/SqlTest/SqlTest/Form1.cs 18 97 SqlTest

    解决办法:

          在Visual Studio 中点击菜单项“视图->解决方案资源管理器”,在其中点开“引用”文件夹,在"Microsoft.Office.Interop.Word" 上点击鼠标右键,选择“属性”,将属性中的“嵌入互操作类型”的值改为“false”即可。


    最新回复(0)