1
votes

I am able to successfully embed HTML text by copying to the Clipboard (thanks to Mike Stall's .NET Debugging Blog) and paste into the body of an Outlook email message.

However, "special" Unicode characters are not getting pasted in properly using the Word.Selection's PasteAndFormat(), PasteSpecial(), or Past() methods. Everything works perfectly unless I have text like this: "Ôh my gôd".

Before the Paste() operation, I can get the clipboard text and see that the correct text has been put into the buffer, and I know the font supports these characters because I can do a copy and paste to the embedded HTML in the email body after my code executes (and it looks fine), but I can't seem to Paste() text with these "special characters" into a Word.Selection. I'm using C#, Visual Studio 2010, and Outlook 2010, fyi.

Thanks in advance for help.

-Eric

1

1 Answers

0
votes

I think the easiest answer would be to ensure that you are escaping all non-ascii characters in the html. This simply avoids the underlying issue which is the copy/paste of unicode character data.

I don't know how you are building the HTML to start with, but all text and attribute values will need to be encoded with the following method.

public static string HtmlEncode(string text, bool isAttribute)
{
    if (text == null)
        return null;

    StringBuilder sb = new StringBuilder(text.Length + 100);

    int len = text.Length;
    for (int i = 0; i < len; i++)
    {
        char ch = text[i];

        switch (ch)
        {
            case '\r':
            case '\n':
                if (isAttribute)
                    goto default;
                sb.Append(ch);
                break;
            case '<':
                sb.Append("&lt;");
                break;
            case '>':
                sb.Append("&gt;");
                break;
            case '&':
                sb.Append("&amp;");
                break;
            default:
                if (ch < 32 || ch > 127 || ch == '\'' || ch == '"')
                {
                    sb.Append("&#");
                    sb.Append(((int)text[i]).ToString(CultureInfo.InvariantCulture));
                    sb.Append(";");
                }
                else
                    sb.Append(text[i]);
                break;
        }
    }
    return sb.ToString();
}