load word file (.docx) in richtextbox

后端 未结 2 1323
無奈伤痛
無奈伤痛 2021-01-24 10:08

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)
{
     // O         


        
2条回答
  •  清歌不尽
    2021-01-24 10:18

     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");
                }
            } 
    

提交回复
热议问题