1

I have a RichTextBox in which I write text and I give it color and format and what I want is that when I press a button that I have programmed, I open the Word application and pass the text that I have in the RichTextBox to the Word document, along with the color and format that I have given in my application.

I have the following code that opens Word and it passes the text that I had in the RichTextBox but the problem is that it does not show me the color and format that I had in the text in my application.

colorLetra = new ColorDialog();
objWord = new Word.Application();
objWord.Visible = true;
objDocumento = objWord.Documents.Add(Missing.Value);
objWord.Selection.Font.Color = objWord.Selection.Font.Color;
objWord.Selection.TypeText(richTextBox.Text);

Could you tell me why it does not show me the color and format in Word?

2

1 Answer 1

1

Your question is:

Could you tell me why it does not show me the color and format in Word?

The reason is because you only enter/type the text. You don't apply any formatting. You're simply transferring the string value of the Windows Forms control to the Word document, as a string.

Your implied question is: How do I pass formatted RichTextBox content to Word...

There is no way to directly pass formatted information from a Windows Form to a Word document. You must go over the clipboard, as was suggested in a comment. The code that comments points to, however, is incorrect for formatted text. The following works for me:

    if (richTextBox.Text.Length > 0)
    {
        // Copy the formatted content to the clipboard
        Clipboard.SetText(richTextBox.Rtf, TextDataFormat.Rtf);
        objWord.Selection.Paste();
    }
1
  • I just tried it and it works. Thanks for the correction and for the help.
    – chuches
    Commented Jun 13, 2018 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.