在RichTextBox中加载Word文件(.docx) [英] load word file (.docx) in richtextbox

查看:802
本文介绍了在RichTextBox中加载Word文件(.docx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够将.docx文件加载到我的wpf应用程序中,但是它似乎没有显示在我的Richtextbox中:

I was already able to load a .docx file into my wpf application but it doesn't seem to show up in my richtextbox:

if (openFile.ShowDialog() == true)
{
     // Open document 
     string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

     if (openFile.CheckFileExists)
     {
         var document = DocX.Load(originalfilename);
         string contents = document.Text;
         rtfMain.Document = contents; 

         MessageBox.Show("file loaded");
     }
} 

wpf中的richtextbox不接受内容字符串变量.知道如何使它起作用吗?

The contents string variable is not accepted by the richtextbox in wpf. Any idea how to make it work?

推荐答案

 if (openFile.ShowDialog() == true)
        {
            // Open document 
            string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

            if (openFile.CheckFileExists && new[] { ".docx", ".doc", ".txt", ".rtf" }.Contains(Path.GetExtension(originalfilename).ToLower()))
            {
                Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application();
                object File = originalfilename;
                object nullobject = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();
                wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
                Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject);
                docs.ActiveWindow.Selection.WholeStory();
                docs.ActiveWindow.Selection.Copy();
                rtfMain.Document.Paste();
                docs.Close(ref nullobject, ref nullobject, ref nullobject);
                wordobject.Quit(ref nullobject, ref nullobject, ref nullobject);


                MessageBox.Show("file loaded");
            }
        } 

这篇关于在RichTextBox中加载Word文件(.docx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆