private void button1_Click(object sender, System.EventArgs e)
{
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)
{
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();
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;
}
}
}
}
please ask why data is null in this program? i write the filename exactly. thank you!
novel
2013-1-10