Copying word document content to a rich text box without losing format
- I am trying to copy the contents of a word document which is basically in a letter format.
I am trying to copy it onto a rich text box editor...but when i copy it..i am losing all the formatting of the word document.
How do i solve this?
Answers
- Hi sjonna
If you use Ctrl+A, Ctrl+C in the Word document, as an end-user; then switch to your RichText box and use Ctrl+V: Do you get the result you're looking for?
It's important to realize that a Word document is not HTML or RTF. It's a proprietary, binary file format. When you copy to the clipboard the information is converted to HTML or RTF. But the result might not be what you expect.
If that works, then you need to adjust your code. The CopyFormat command does not "copy text with formatting". It is Word's internal Format Painter tool that copies formatting applied to a selection, then pastes that formatting to another text selection - within Word (only).
Try just the Copy method.
Cindy Meister, VSTO/Word MVP- Marked As Answer byCindy MeisterMVP, ModeratorFriday, November 06, 2009 8:08 AM
Hello,
Have you tried Copy method Cindy suggested above. In my side, I use this method on Range object rather Selection object. According to your code, you are using Selection object. However, I think it is same for us to use Copy method on these two objects [See: Range.Copy method and Selection.Copy method] to copy the text with format to Clipboard. And then use Paste method on rich text box [See: RichTextBox.Paste Method]. Here is the code,
Word.Application wordApp = new Word.Application();
object filename=@"C:\Temp\11.docx";
object missing=Type.Missing;
Word.Document doc = wordApp.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToAbsolute;
object count = 1;
object sentence = Word.WdUnits.wdSentence;
doc.ActiveWindow.Selection.GoTo(ref what, ref which, ref count, ref missing);
object index = "\\Page";
Word.Range rng = doc.Bookmarks.get_Item(ref index).Range;
rng.Copy();
richTextBox1.Paste();
doc.Close(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(wordApp);
For test, I used GoTo method to Page 1, and then get the range representing the whole page.
If you have any further question, please feel free to let us know.
Best regards,
Bessie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byCindy MeisterMVP, ModeratorFriday, November 06, 2009 8:08 AM
All Replies
- This is the code i was using
Dim aDoc As Word.Document = WordApp.Documents.Open(outfile, missing, [readOnly], missing, missing, missing, missing, missing, missing, missing, missing, isVisible)
aDoc.ActiveWindow.Selection.WholeStory()
aDoc.ActiveWindow.Selection.CopyFormat()
Rte1.Text = aDoc.ActiveWindow.Selection.Text()
aDoc.Close()
WordApp.Application.Quit()
- Hi sjonna
If you use Ctrl+A, Ctrl+C in the Word document, as an end-user; then switch to your RichText box and use Ctrl+V: Do you get the result you're looking for?
It's important to realize that a Word document is not HTML or RTF. It's a proprietary, binary file format. When you copy to the clipboard the information is converted to HTML or RTF. But the result might not be what you expect.
If that works, then you need to adjust your code. The CopyFormat command does not "copy text with formatting". It is Word's internal Format Painter tool that copies formatting applied to a selection, then pastes that formatting to another text selection - within Word (only).
Try just the Copy method.
Cindy Meister, VSTO/Word MVP- Marked As Answer byCindy MeisterMVP, ModeratorFriday, November 06, 2009 8:08 AM
- //If you use Ctrl+A, Ctrl+C in the Word document, as an end-user; then switch to your RichText box and use Ctrl+V: Do you get the result you're looking for?
//
Yes,when i paste after ctrl+A,ctrl+C and do a paste on my rich text editor,the formating is not lost.
But when i do from programming using the code i pasted above,the formatting is lost.
I even tried using copy instead of copyformat. //If you use Ctrl+A, Ctrl+C in the Word document, as an end-user; then switch to your RichText box and use Ctrl+V: Do you get the result you're looking for?
//
Yes,when i paste after ctrl+A,ctrl+C and do a paste on my rich text editor,the formating is not lost.
But when i do from programming using the code i pasted above,the formatting is lost.
I even tried using copy instead of copyformat.
When you tested using the keyboard, as an end-user, did you also close the document before pasting? It's possible that the amount of information you're copying does not fit on the Clipboard, so that it's holding a link open to the original selection. If the document is then closed, the link (and thus the information) is lost. Try NOT closing the document in your code and see if that makes a difference.
Cindy Meister, VSTO/Word MVP- Hi,
In either case(doc opened/doc closed) when i copy paste the contents from a word document onto my rich text editor control,I am able to paste the contents with the same format as the word doc.
I just need to do it from my .net code. - any updates on this please?
i am waiting on this for me to proceed with my work..any immediate help regarding would be greatly appreciated... - Can some one give some idea on this please?
Hello,
Have you tried Copy method Cindy suggested above. In my side, I use this method on Range object rather Selection object. According to your code, you are using Selection object. However, I think it is same for us to use Copy method on these two objects [See: Range.Copy method and Selection.Copy method] to copy the text with format to Clipboard. And then use Paste method on rich text box [See: RichTextBox.Paste Method]. Here is the code,
Word.Application wordApp = new Word.Application();
object filename=@"C:\Temp\11.docx";
object missing=Type.Missing;
Word.Document doc = wordApp.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
object what = Word.WdGoToItem.wdGoToPage;
object which = Word.WdGoToDirection.wdGoToAbsolute;
object count = 1;
object sentence = Word.WdUnits.wdSentence;
doc.ActiveWindow.Selection.GoTo(ref what, ref which, ref count, ref missing);
object index = "\\Page";
Word.Range rng = doc.Bookmarks.get_Item(ref index).Range;
rng.Copy();
richTextBox1.Paste();
doc.Close(ref missing, ref missing, ref missing);
Marshal.ReleaseComObject(wordApp);
For test, I used GoTo method to Page 1, and then get the range representing the whole page.
If you have any further question, please feel free to let us know.
Best regards,
Bessie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byCindy MeisterMVP, ModeratorFriday, November 06, 2009 8:08 AM
- Thanks Bessie for the reply.
Is Richtextbox available for web forms as well?
Does the above code work for Web applications as well? Hello again,
As far as I know, RichtextBox can be placed on the Web Form. Here is the link which may interest you: http://msdn.microsoft.com/en-us/library/aa984362(VS.71).aspx. However, I cannot ensure that there is a method copying to Clipboard of Web applications or copying the format.
<<Is Richtextbox available for web forms as well? Does the above code work for Web applications as well?>>
It seems that this issue is not related to VSTO technology. Since I am not a web forms expert, I don't know what all is going on behind the scenes, here. If this post does not help you, I recommend you post this issue to Web Forms forum: http://forums.asp.net/18.aspx, where you would get probably more help.
Best regards,
Bessie
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- I already posted there,with no replies.
- Hi sjonna
<<I already posted there,with no replies>>
You might have to wait for a day or two for a reply. The forums are manned in part by voluteers who answer questions in the spare time.
FWIW web applications are off-topic in the VSTO forum. You'll find more information about what VSTO is, and what the forum supports, in the Please Read First message. That message also has links for venues for non-VSTO, Office-related questions. We've helped you as far as we can, here. You'll need to pursue your requirments in another venue.
Cindy Meister, VSTO/Word MVP - Sure Cindy.
Thanks a lot for the help.


