none
How to import a documents(docx) to richtextbox in C#

    Question

  • I have already search a lot of codes in the Google on how import a documents(.docx) to the richtextbox, but the only available is from the richtextbox to the Microsoft office. I'm looking for someone that will help of my problem. And also when you create a documents to the richtextbox then you save it the file will be .docx and not a .rtf. Please help me :(
    Friday, January 24, 2014 2:47 PM

All replies

  • Hi,

    Actually I have a simple example for you, The example works perfectly.

    1. Add Reference (Microsoft.Office.Interop.Word)
    2. Add this statement " using Word = Microsoft.Office.Interop.Word; "

    Import word document:

    void ImportWord() { Microsoft.Office.Interop.Word.ApplicationClass wordObject = new Microsoft.Office.Interop.Word.ApplicationClass(); object File = "c:\\Test.docx"; //this is the path 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(); this.richTextBox1.Paste(); docs.Close(ref nullobject, ref nullobject, ref nullobject); wordobject.Quit(ref nullobject, ref nullobject, ref nullobject); }

    Export word document:

            void ExportToWord()
            {
                Word.Application objApp = default(Word.Application);
                Word.Document objDoc = default(Word.Document);
                object missing = System.Reflection.Missing.Value;
                objApp = new Word.Application();
                object File = "c:\\Test.docx"; //this is the path
    
                objDoc = objApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                objDoc.Activate();
                objApp.Selection.TypeText(richTextBox1.Text);
                objDoc.SaveAs(ref File, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                objDoc.Close(ref missing, ref missing, ref missing);
                objApp.Quit(ref missing, ref missing, ref missing);
            }

    private void button1_Click(object sender, EventArgs e) { ImportWord(); } private void button2_Click(object sender, EventArgs e) { ExportToWord(); }

    Hope it helps..

    Michel,



    • Edited by Michel.Sy Friday, January 24, 2014 5:57 PM
    Friday, January 24, 2014 5:51 PM
  • I've tried this code but their's an error when I'm run the program .

    

     objDoc.SaveAs(ref File, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    

    Sunday, January 26, 2014 3:51 PM
  • Hi again,
    I'm sorry to say that, but I tested my code much more and it working perfectly.

    My solution, Please download it here:


    • Edited by Michel.Sy Sunday, January 26, 2014 11:35 PM
    Sunday, January 26, 2014 5:14 PM
  • I already downloaded your solution but it the same problem when I'm going to run :( . What version of visual studio you have used ?
    Monday, January 27, 2014 1:27 AM
  • Hello,

       You can use free spire.doc to this without install office,
        //Create word document
        Document document = new Document();
        document.LoadFromFile(@"Sample.docx");
    
        //Save doc file.
        document.SaveToFile("Sample.rtf", FileFormat.Rtf);
        richTextbox.LoadFile("Sample.rtf");

    Monday, January 27, 2014 2:37 AM
  • Michel;

     Excellent post on converting DOC files to RTF... One question?

    Can you do this without Word interop showing the Word window?


    JP Cowboy Coders Unite!

    Monday, January 27, 2014 4:32 AM