C#打开WORD文档内容并显示代码如下:
//在项目引用里添加上对Microsoft Word object library的引用
private void button_Click(object sender SystemEventArgs e)
{
//调用打开文件对话框获取要打开的文件WORD文件RTF文件文本文件路径名称
OpenFileDialog opd = new OpenFileDialog();
opdInitialDirectory = \c:\\\\\;
opdFilter = \Word文档(*doc)|*doc|文本文档(*txt)|*txt|RTF文档(*rtf)|*rtf|所有文档(**)|**\;
opdFilterIndex = ;
if (opdShowDialog() == DialogResultOK && opdFileNameLength > )
{
//建立Word类的实例缺点:不能正确读取表格图片等等的显示
WordApplicationClass app = new WordApplicationClass();
WordDocument doc = null;
object missing = SystemReflectionMissingValue;
object FileName = opdFileName;
object readOnly = false;
object isVisible = true;
object index = ;
try
{
doc = appDocumentsOpen(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);
docActiveWindowSelectionWholeStory();
docActiveWindowSelectionCopy();
//从剪切板获取数据
IDataObject data=ClipboardGetDataObject();
thisrichTextBoxText=dataGetData(DataFormatsText)ToString();
}
finally
{
if (doc != null)
{
docClose(ref missing ref missing ref missing);
doc = null;
}
if (app != null)
{
appQuit(ref missing ref missing ref missing);
app = null;[Page]
}
}
}
}