944,149 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 9416
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 12th, 2009
0

how to load .doc file into richTextBox

Expand 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.
Similar Threads
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007
Oct 12th, 2009
0
Re: how to load .doc file into richTextBox
you can read the contents of any file to a text box with the string reader class

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Oct 13th, 2009
0
Re: how to load .doc file into richTextBox
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
Reputation Points: 8
Solved Threads: 3
Junior Poster
MxDev is offline Offline
137 posts
since Sep 2007
Oct 13th, 2009
0
Re: how to load .doc file into richTextBox
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

C# Syntax (Toggle Plain Text)
  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
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Oct 13th, 2009
0
Re: how to load .doc file into richTextBox
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.
Reputation Points: 31
Solved Threads: 21
Junior Poster in Training
jatin24 is offline Offline
74 posts
since Aug 2009
Oct 14th, 2009
0
Re: how to load .doc file into richTextBox
Click to Expand / Collapse  Quote originally posted by MxDev ...
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:
C# Syntax (Toggle Plain Text)
  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...............
Reputation Points: 31
Solved Threads: 36
Posting Whiz
avirag is offline Offline
312 posts
since Jun 2009
Oct 14th, 2009
0
Re: how to load .doc file into richTextBox
Mark this thread as solved if it help you...........
Reputation Points: 31
Solved Threads: 36
Posting Whiz
avirag is offline Offline
312 posts
since Jun 2009
Oct 14th, 2009
0
Re: how to load .doc file into richTextBox
Click to Expand / Collapse  Quote originally posted by avirag ...
Well you can use this on button click event:
C# Syntax (Toggle Plain Text)
  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.)
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Nov 10th, 2009
0
Re: how to load .doc file into richTextBox
you can read the contents of any file to a text box with the string reader class

C# Syntax (Toggle Plain Text)
  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; Nov 10th, 2009 at 2:32 pm.
Reputation Points: 537
Solved Threads: 398
Postaholic
Mitja Bonca is offline Offline
2,010 posts
since May 2009
Nov 10th, 2009
0
Re: how to load .doc file into richTextBox
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.
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Weird error
Next Thread in C# Forum Timeline: Help For System Message Handling





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC