日期:2014-05-17 浏览次数:20882 次
private void button1_Click(object sender, EventArgs e) { object filepath= "D:\\Debugtest.doc"; openWord(filepath); } //把Word文档内容取出来放到richtextBox里 private void openWord(object SPath) { object file = SPath; object nullobj = System.Reflection.Missing.Value; Word.Document doc = myWordApp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,ref nullobj,ref nullobj,ref nullobj,ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); this.richTextBox1.Text = data.GetData(DataFormats.Text).ToString(); doc.Close(ref nullobj, ref nullobj, ref nullobj); }
------解决方案--------------------
using Word = Microsoft.Office.Interop.Word; namespace TestForms { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Word.ApplicationClass wordApp = new Word.ApplicationClass(); object file = @"C:\Users\aaa\Desktop\生产经营管理系统需求.docx"; object nullobj = System.Reflection.Missing.Value; Word.Document doc = wordApp.Documents.Open( ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data = Clipboard.GetDataObject(); //读取word中的文本 string mytext = data.GetData(DataFormats.Text).ToString(); doc.Close(); } } }