Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, i want to open word file in a rich text box with images,bullets and texts
and then i want line by line sentences.
I am using this code-

C#
Microsoft.Office.Interop.Word.ApplicationClass wordObject = new Microsoft.Office.Interop.Word.ApplicationClass(); 
object File = txtfilepath.Text; //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(); 
IDataObject data = Clipboard.GetDataObject(); 
rtbgermanfile.Text = data.GetData(DataFormats.Rtf).ToString();
 string name = rtbgermanfile.Text.Trim(); name = String.Join(Environment.NewLine, name.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)); 
string newtext = name.Replace("\n\n", "\n");
 string newtext2 = newtext.Replace("\n\n", "\n");
 rtbgermanfile.Text = newtext2.ToString(); 
txtwriteingerman.Text = rtbgermanfile.Lines[0]; 
docs.Close(ref nullobject, ref nullobject, ref nullobject);
 wordobject.Quit(ref nullobject, ref nullobject, ref nullobject);
 //wordobject.Quit(SaveChanges:false, OriginalFormat: false, RouteDocument: false); 
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordobject); txtwriteinenglish.Focus();

it works fine but not able to open images.
its urgent
please help me out.

Thanks in Advance.
Posted
Updated 29-Feb-12 21:38pm
v2
Comments
Varun Sareen 1-Mar-12 3:38am    
have you tried google?
mayankshrivastava 1-Mar-12 3:41am    
of cos firstly i search in Google, then only i uploaded a question here...

1 solution

The class RichTextBox supports only RTF format, and pretty purely. There is a dirty way to insert a picture in it using clipboard:

For example:
C#
Image img = Image.FromFile("myImage.jpg");
Clipboard.SetImage(img);
myRichTextBox.SelectionStart = 0; //for example; you need some position to insert it
myRichTextBox.SelectionLength = 0;
myRichTextBox.Paste();
However, you can face too many problems with that.

I would advice you to use some alternative. Instead of Rich Text Format, you could use HTML. I would recommend this wonderful component offered in this CodeProject article:

A Professional HTML Renderer You Will Use[^].

The work I referenced above is excellent! Why do you need such brain damage as RTF is you can use very rich HTML? The author of this work developed such a wonderful thing as a support for special URL schema to include images and other resources from the resource embedded in the executable. This is a nearly perfect method of presentation of structured read-only documents without using of the WebBrowser control.

One more alternative: use WPF. It has excellent support for Rich documents and XPS. Please see my recent answer on XPS viewer:
The XPS Document Viewer Sample In Intersoft ClientUI[^].

—SA
 
Share this answer
 
Comments
mayankshrivastava 1-Mar-12 6:39am    
Thanks for reply SAKryukov,
actually my project is that i want to open a word file in a text box or rich text box with images and texts.Then i want to all sentences starting from new line then want to read line by line sentences.
Sergey Alexandrovich Kryukov 1-Mar-12 20:30pm    
Well, Word format is not directly supported, but you can read data from Word document and present it somehow (perhaps in somewhat simplified manner) as RTF or HTML (I'm telling you, it's a batter solution).

Anyway, when and if you agree it makes sense, consider accepting this answer formally (green button) -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900