how to load .doc file into richTextBox

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 63
Reputation: MxDev has a little shameless behaviour in the past 
Solved Threads: 1
MxDev MxDev is offline Offline
Junior Poster in Training

how to load .doc file into richTextBox

 
0
  #1
Oct 12th, 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 .doc into richTextBox object?????

any help appreciated. Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 329
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
0
  #2
Oct 12th, 2009
you can read the contents of any file to a text box with the string reader class

  1. using (System.IO.StreamReader sr = new System.IO.StreamReader("TestFile.txt"))
  2. {
  3. RichTextBox1.Text = sr.ReadToEnd();
  4. }

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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 63
Reputation: MxDev has a little shameless behaviour in the past 
Solved Threads: 1
MxDev MxDev is offline Offline
Junior Poster in Training
 
0
  #3
Oct 13th, 2009
Thanks for your reply, but I think there's no need to use the StreamReader class here because the richTextBox.LoadFile() support this inherently.

This method I think it won't load .doc file because it may contain some object and it won't read it correctly.

So it still unsolved.

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 329
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
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

  1. Word.ApplicationClass wordApp=new ApplicationClass();
  2. //Word.ApplicationClass is to access the word application
  3.  
  4. object file=path;
  5.  
  6. object nullobj=System.Reflection.Missing.Value;
  7.  
  8. Word.Document doc = wordApp.Documents.Open(
  9.  
  10. ref file, ref nullobj, ref nullobj,
  11.  
  12. ref nullobj, ref nullobj, ref nullobj,
  13.  
  14. ref nullobj, ref nullobj, ref nullobj,
  15.  
  16. ref nullobj, ref nullobj, ref nullobj);
  17.  
  18. doc.ActiveWindow.Selection.WholeStory();
  19.  
  20. doc.ActiveWindow.Selection.Copy();
  21.  
  22. IDataObject data=Clipboard.GetDataObject();
  23.  
  24. txtFileContent.Text=data.GetData(DataFormats.Text).ToString();
  25.  
  26. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 70
Reputation: jatin24 is an unknown quantity at this point 
Solved Threads: 19
jatin24's Avatar
jatin24 jatin24 is offline Offline
Junior Poster in Training
 
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 210
Reputation: avirag is an unknown quantity at this point 
Solved Threads: 15
avirag avirag is offline Offline
Posting Whiz in Training
 
0
  #6
Oct 14th, 2009
Originally Posted by MxDev View Post
Hi guys,
How to load any other document formats other than .txt , and .rtf (which naturally supported by LoadFile() function), like .java, .c# , and .doc into richTextBox object?????

any help appreciated. Thanks in advance.
Well you can use this on button click event:
  1. OpenFileDialog f = new OpenFileDialog();
  2. f.Title = "open file as..";
  3. f.Filter = "Doc Files|*.doc|Java Files|*.java|C# Files|*.cs|All Files|*.*"; // and in a similar way you can load any format here.......
  4. DialogResult dr = f.ShowDialog();
  5. if (dr == DialogResult.OK)
  6. {
  7. s1 = f.FileName;
  8. richTextBox1.LoadFile(s1);
  9. open=true;
  10. }
By the help of this you can load any other doument format files as well...............
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 210
Reputation: avirag is an unknown quantity at this point 
Solved Threads: 15
avirag avirag is offline Offline
Posting Whiz in Training
 
0
  #7
Oct 14th, 2009
Mark this thread as solved if it help you...........
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 329
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
0
  #8
Oct 14th, 2009
Originally Posted by avirag View Post
Well you can use this on button click event:
  1. OpenFileDialog f = new OpenFileDialog();
  2. f.Title = "open file as..";
  3. f.Filter = "Doc Files|*.doc|Java Files|*.java|C# Files|*.cs|All Files|*.*"; // and in a similar way you can load any format here.......
  4. DialogResult dr = f.ShowDialog();
  5. if (dr == DialogResult.OK)
  6. {
  7. s1 = f.FileName;
  8. richTextBox1.LoadFile(s1);
  9. open=true;
  10. }
By the help of this you can load any other doument format files as well...............

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.)
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 54
Reputation: Mitja Bonca is an unknown quantity at this point 
Solved Threads: 0
Mitja Bonca Mitja Bonca is offline Offline
Junior Poster in Training
 
0
  #9
22 Days Ago
Originally Posted by Diamonddrake View Post
you can read the contents of any file to a text box with the string reader class

  1. using (System.IO.StreamReader sr = new System.IO.StreamReader("TestFile.txt"))
  2. {
  3. RichTextBox1.Text = sr.ReadToEnd();
  4. }

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.
Is this true? Look at this picture:
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 329
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz
 
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC