954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

RichTextBox to MS Word (with formatting)

Hi,

Im trying to transfer some text from the richTextBox into an existing MS word template at a specific location.
I am able to transfer text to any location i like, but i want to retain the formatting of the text from the richTextBox.

Im trying to copy the text to the clipboard, and pasting it in Word, but its not working for some reason.

Microsoft.Office.Interop.Word.Selection sel = oWord.Selection;
Clipboard.Clear();                    
//richTextBox4.SelectAll();
Clipboard.SetDataObject(richTextBox4.Rtf, true);                        
                    
IDataObject data = Clipboard.GetDataObject();                    
sel.InsertBefore(data.GetData(DataFormats.Rtf).ToString());


Can anyone tell me how to fix this, or is there any other way to transfer a chunk of text from richTextBox to MS Word without loosing the formatting.
please note: i know i can directly save data from richTextBox to an rtf or doc file.. i want to insert the data in an existing template.

thanks!

jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
 

Hello.
I was playing with your code. Here's what I've got:

// selecting some text in RichTextBox
...
Clipboard.SetText(richTextBox1.SelectedRtf, TextDataFormat.Rtf);
sel.Paste();
Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 

Hey thanks for that! It works!!

jatin24
Junior Poster in Training
75 posts since Aug 2009
Reputation Points: 31
Solved Threads: 21
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: