Hi i am open a word file 2007(.doc file) in a rich text box through open dialog box.
It works fine but images and bullets are not shown in a rich text box.
i want to open open word file with images and bullets in a rich text box.

I am using this code for open a word file and convert it to sentence by sentence.

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.Text).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);

Thanks in Advance.

Dani AI

Generated

Short answer: don't copy plain text to the clipboard — convert the document to RTF (or grab the RTF clipboard format) and load that into the RichTextBox. The WinForms RichTextBox understands RTF (including inline images and bullets) but DataFormats.Text strips formatting and pictures. your current clipboard approach uses the text format; that is why images and bullets disappear. 's JavaScript/RTF idea applies to web pages, not to a WinForms RichTextBox.

A simple, reliable pattern is: ask Word to save the document as RTF, then call RichTextBox.LoadFile on the temp RTF. That preserves most images and bullet formatting and avoids pasting plain text:

var word = new Microsoft.Office.Interop.Word.Application();
var doc = word.Documents.Open(filePath, ReadOnly: true, Visible: false);
string tempRtf = Path.ChangeExtension(Path.GetTempFileName(), ".rtf");
doc.SaveAs(tempRtf, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF);
doc.Close();
word.Quit();

richTextBox1.LoadFile(tempRtf, RichTextBoxStreamType.RichText);
File.Delete(tempRtf);

If you prefer using the clipboard, copy the document range and read the RTF clipboard format instead of text:

doc.Content.Copy();
var data = Clipboard.GetDataObject();
if (data.GetDataPresent(DataFormats.Rtf))
    richTextBox1.Rtf = (string)data.GetData(DataFormats.Rtf);

Troubleshooting / cautions: release COM objects (Marshal.ReleaseComObject), wrap Interop calls in try/finally, and avoid Office interop on servers (not supported). Some Word features (floating shapes, SmartArt or complex OLE objects) may not translate to RTF — if fidelity is critical, consider a dedicated converter library (Aspose/GemBox/Spire) or render HTML and use a browser control. Also note your original code creates/uses multiple Application variables — use a single Application instance and dispose it cleanly.

Recommended Answers

All 3 Replies

try Google

java script of rtf get script and use it that will work i got same solution some months back

Hi guys,
I would like to know how the split function is working. The following is the problem and i want to solve it.

I am getting input from the user as 200*569+875-9632

the thing is i have to split the integer and perform the operation as above.

Any suggestions,

Thanks in advance.

Hi, vinoit.
Welcome to DaniWeb.
For your question, you will need to start a new thread.
On the main forum page near the bottom, there is a large button that says "Start New Thread".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.