private void button1_Click(object sender, System.EventArgs e) { //Invoke and open file dialog to get Word, RTF, text file path name OpenFileDialog opd = new OpenFileDialog(); opd.InitialDirectory = "c:\\"; opd.Filter = "Word文档(*.doc)|*.doc|Text(*.txt)|*.txt|RTF File(*.rtf)| *.rtf|All files(*.*)|*.*"; opd.FilterIndex = 1; if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0) { // Create Word class sample Word.ApplicationClass app = new Word.ApplicationClass(); Word.Document doc = null; object missing = System.Reflection.Missing.Value; object FileName = opd.FileName; object readOnly = false; object isVisible = true; object index = 0; try { doc = app.Documents.Open(ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); // Get data from clipboard IDataObject data=Clipboard.GetDataObject(); this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString(); } finally { if (doc != null) { doc.Close(ref missing, ref missing, ref missing); doc = null; } if (app != null) { app.Quit(ref missing, ref missing, ref missing); app = null; } } } }
data
data.GetData(DataFormats.Text)
null
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)