Copy formatted content from a Visual Studio RichTextBox to Word

One question I’ve seen fairly regularly is, “How do I copy formatted content from a Visual Studio RichTextBox to a Word document?”

The answer, of course, is, “Use the clipboard.” But behind that is another question: How to put the content in the Clipboard?

I was lucky enough to come across the question How to pass text formatted from a Windows Form to the Word application? on Stack Overflow. In a comment to the question a user posted a link to a similar discussion, which had been closed because the question is inadequately stated. But luckily enough, someone posted an answer to it before it was closed, mentioning how to put content from a Windows Form control into the Clipboard using Clipboard.SetText. The arguments given were for plain text, however, not rich text.

Coming from the Word side of the tracks, my knowledge about Visual Studio is spotty (to put it mildly). I was very happy to see this hint and researching it I came up with the correct syntax to put formatted text in a Windows Form control on the Clipboard, then paste it into Word:

if (richTextBox.Text.Length > 0)
{
    // Copy the formatted content to the clipboard
    Clipboard.SetText(richTextBox.Rtf, TextDataFormat.Rtf);
    objWord.Selection.Paste();
}

It seems almost trivial, but in all these years I’ve never seen this information posted in answer to the question. So I’m sharing it here…



2 Responses to “Copy formatted content from a Visual Studio RichTextBox to Word”

  1.   Peter Jamieson Says:

    This is very useful.

    For anyone wishing to insert a piece of Rtf while leaving the clipboard untouched, the most obvious option would be to save the Rtf into a file, then insert the file via., e.g., the Selection.InsertFile or Range.InsertFile methods.

    But if you want to avoid altering the clipboard *and* saving to an external file there is another possible approach:
    a. Build an Office Open format document in memory
    b. Use an Rtf format Altchunk to insert the Rtf
    c. Use the Selection.InsertXML or Range.InsertXML method to insert the document

    The code looks like this. I mention a couple of caveats later.

    To insert the Rtf you would use a call something like this

    myrange.InsertRtf(FlatOpcWithRtfAltChunk(myrichtextbox.Rtf));

    My range is the Word Range, myrichtextbox is the Windows Forms RichTextBox, and FlatOpcWithRtfAltChunk looks like this:

    private string FlatOpcWithRtfAltChunk(string text)
    {
    const string opcStart = "@";
    const string opcFinish = "@";
    return opcStart + System.Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(text), Base64FormattingOptions.InsertLineBreaks) + opcFinish;
    }

    Notice that this code does not rely on the Office Open XML SDK, although you could build the initial document using that if you preferred.

    There are some caveats:
    1. .InsertXML does not always work. Sometimes, it’s because Word will not let you insert into the specified Selection or Range. (One situation might be where the selection/range includes a table end-of-cell marker).
    2. It is not completely clear in the general case how to ensure that the Rtf that comes back from the Rich Text Box should be encoded into Base64 for the AltChunk. e.g., here the RichText that comes back starts “{\rtf1\ansi\ansicpg1252”, and I believe that the “Encoding.Default” used in the return statement above will also specify Code Page 1252 (i.e. .NET may use Unicode encoding internally for strings by default, but the Encoding.Default is actually picked up from your Regional Settings). However, I have seen suggestions that that may not be the case for all versions of .Net, e.g. .Net Core. However, if that is not enough, it should be possible to determine the encoding from the RTF Header and specify that Encoding instead. Not something I have explored.

Leave a Reply