| | |
how to load .doc file into richTextBox
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
0
#2 Oct 12th, 2009
you can read the contents of any file to a text box with the string reader class
but .doc files will not load as plain text because they are a special format created by Microsoft word. But if you have Microsoft word installed on the computer you are using your C# app on you can load up the Microsoft Word Object Library by adding it into the reference and create an object for reading .doc files.
but the streamreader will read most text formats.
C# Syntax (Toggle Plain Text)
using (System.IO.StreamReader sr = new System.IO.StreamReader("TestFile.txt")) { RichTextBox1.Text = sr.ReadToEnd(); }
but .doc files will not load as plain text because they are a special format created by Microsoft word. But if you have Microsoft word installed on the computer you are using your C# app on you can load up the Microsoft Word Object Library by adding it into the reference and create an object for reading .doc files.
but the streamreader will read most text formats.
0
#4 Oct 13th, 2009
I don't think you caught most of my post. the Stream reader class will let you load text that loadfile won't.
.DOC is a proprietary format!
this means to edit it you need access to the original software that it belongs to. microsoft allows you to interlop methods from its office suite in other applications.
I.E. If you have Microsoft office installed on the computer you run your c# application on, then you can open .doc files, Otherwise you cannot!
assuming you do have Microsoft office installed on your developer machine must first add a reference to Microsoft Word Object Library then just use the code
NOTE: .doc files are a zip compressed special formatted XML file, but getting it right manually would be virtually impossible because there are possible hundreds of special formatting commands. So even though technically you could decompress and parse the document files manually, you would spend months getting it to work right. Better to just target machines with MS Word
.DOC is a proprietary format!
this means to edit it you need access to the original software that it belongs to. microsoft allows you to interlop methods from its office suite in other applications.
I.E. If you have Microsoft office installed on the computer you run your c# application on, then you can open .doc files, Otherwise you cannot!
assuming you do have Microsoft office installed on your developer machine must first add a reference to Microsoft Word Object Library then just use the code
C# Syntax (Toggle Plain Text)
Word.ApplicationClass wordApp=new ApplicationClass(); //Word.ApplicationClass is to access the word application object file=path; object nullobj=System.Reflection.Missing.Value; Word.Document doc = wordApp.Documents.Open( ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); IDataObject data=Clipboard.GetDataObject(); txtFileContent.Text=data.GetData(DataFormats.Text).ToString(); doc.Close();
NOTE: .doc files are a zip compressed special formatted XML file, but getting it right manually would be virtually impossible because there are possible hundreds of special formatting commands. So even though technically you could decompress and parse the document files manually, you would spend months getting it to work right. Better to just target machines with MS Word
0
#5 Oct 13th, 2009
I had to do something similar about a week ago and did something very similar what Diamonddrake suggested. However this method looses all the formatting of the text that was on the doc file.
Is there any way you can preserve that formatting when its copied to the RichTextBox? If you manually select a text, copy and paste, the text pasted on the RichTextBox usually retains the formatting...
I think it would be something to do with the GetData(DataFormats.???) but not sure.
Is there any way you can preserve that formatting when its copied to the RichTextBox? If you manually select a text, copy and paste, the text pasted on the RichTextBox usually retains the formatting...
I think it would be something to do with the GetData(DataFormats.???) but not sure.
•
•
Join Date: Jun 2009
Posts: 210
Reputation:
Solved Threads: 15
0
#6 Oct 14th, 2009
•
•
•
•
Hi guys,
How to load any other document formats other than.txt, and.rtf(which naturally supported by LoadFile() function), like.java, .c#, and.docinto richTextBox object?????
any help appreciated. Thanks in advance.
C# Syntax (Toggle Plain Text)
OpenFileDialog f = new OpenFileDialog(); f.Title = "open file as.."; f.Filter = "Doc Files|*.doc|Java Files|*.java|C# Files|*.cs|All Files|*.*"; // and in a similar way you can load any format here....... DialogResult dr = f.ShowDialog(); if (dr == DialogResult.OK) { s1 = f.FileName; richTextBox1.LoadFile(s1); open=true; }
0
#8 Oct 14th, 2009
•
•
•
•
Well you can use this on button click event:By the help of this you can load any other doument format files as well...............C# Syntax (Toggle Plain Text)
OpenFileDialog f = new OpenFileDialog(); f.Title = "open file as.."; f.Filter = "Doc Files|*.doc|Java Files|*.java|C# Files|*.cs|All Files|*.*"; // and in a similar way you can load any format here....... DialogResult dr = f.ShowDialog(); if (dr == DialogResult.OK) { s1 = f.FileName; richTextBox1.LoadFile(s1); open=true; }
This will work for .cs files and other native text files, but .doc is a proprietary format. It WILL NOT WORK for microsoft word documents. (if you just name a text file file.doc it doesn't not make it a .doc file. it just appears that way, true .doc are zipped special XML files and the ritchtextbox class will not parse it.)
•
•
Join Date: May 2009
Posts: 54
Reputation:
Solved Threads: 0
0
#9 22 Days Ago
•
•
•
•
you can read the contents of any file to a text box with the string reader class
C# Syntax (Toggle Plain Text)
using (System.IO.StreamReader sr = new System.IO.StreamReader("TestFile.txt")) { RichTextBox1.Text = sr.ReadToEnd(); }
but .doc files will not load as plain text because they are a special format created by Microsoft word. But if you have Microsoft word installed on the computer you are using your C# app on you can load up the Microsoft Word Object Library by adding it into the reference and create an object for reading .doc files.
but the streamreader will read most text formats.
http://www.file.si/files/g0f81hvm4fua5fiad6h1.jpg
Here doesn`t look that I can get any text out of .doc file.
Last edited by Mitja Bonca; 22 Days Ago at 2:32 pm.
0
#10 22 Days Ago
as mentioned before, .doc files are ZIP compressed xml files. so if you read the data of a .doc file using the code I posted it will not show you the text it will show you the result of the binary compression expressed as ascii characters.
Sorry. the point of all my posts was to explain that word is a special format that requires an office interlop to read.
Sorry. the point of all my posts was to explain that word is a special format that requires an office interlop to read.
![]() |
Similar Threads
- Load a file to RichTextBox (C#)
- How a doc file can be open inside browser without prompting the download dialog box (ASP.NET)
- saving as .doc file (VB.NET)
- load an xml file,to treeview control throw dhtml (JavaScript / DHTML / AJAX)
- how to upload a doc file into database (ASP.NET)
- search text in a doc file (VB.NET)
- How To Load Text File (JSP)
- Cannot load XML file. (PHP)
Other Threads in the C# Forum
- Previous Thread: Weird error
- Next Thread: Help For System Message Handling
| Thread Tools | Search this Thread |
.net access ado.net algorithm array backup barchart bitmap box broadcast buttons c# check checkbox client clock color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing dynamiccreation encryption enum event excel file form format forms function game gdi+ httpwebrequest image index input install interface java label list listbox mandelbrot math microsystems mouseclick mysql operator password path photoshop picturebox pixelinversion post programming property radians regex remote remoting richtextbox running... serialization server sleep soap socket sql sqlserver stack statistics stream string table text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf write xml





