Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am able open a word file which has no format and only contain text in a rich text box.file is open properly but now i want to open a word file with images and bullets .

how to open this ?

Thanks in Advance-
Posted
Updated 8-Feb-12 0:35am
v4
Comments
mayankshrivastava 13-Feb-12 1:28am    
i am using this code for open a word file-
<pre lang="cs">
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();
</pre>

but it open only text not image.

If you save the word document to a RTF file, you can open it using LoadFile method.

However using a RichTextBox can be quite problematic. Consider using for example http://www.freetextbox.com/[^] and HTML format for the Word contents
 
Share this answer
 
In System.Windows.Forms.RichTextBox, images are not well supported. Anyway, there is a way to insert them using the 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[^].

—SA
 
Share this answer
 
v2
Comments
mayankshrivastava 6-Feb-12 4:06am    
i want to just open a word file in RTB with existing images.
i am using this code to open a word file-
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();

but it open only text .
i want to opne image and bullets.
 
Share this answer
 

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