Search code examples
c#clipboardmicrosoft-teamsrtfclipboarddata

Writing to the clipboard in C# in a way that generates formatting that Microsoft Teams can use


I've got an app that is meant to take a string, and write it into the clipboard at 30 pt font, Courier New. The code below works great for word, outlook, basically anything that accepts RTF formatted text. it isn't being seen as anything but plain text in Teams, unfortunately, which is the most common use case, and the most important by far. Does anyone have any insight on how to format it in a way that MS Teams will recognize?

private void ExecuteCopy(String lap)
        {
            // Escape Special Characters for RTF
            StringBuilder rtf = new StringBuilder();
            StringBuilder html = new StringBuilder();
            html.Append("<p style = \"font - family:'Courier New'; font - size:30px\">");

            foreach (char c in lap)
            {
                rtf.Append(GetRtfEncoding(c));
                html.Append(c);
            }

            html.Append("</p>");

            // Add both RTF and plain Text into clipboard
            DataObject data = new DataObject();
            data.SetData(DataFormats.Text, lap);
            data.SetData(DataFormats.Rtf, @"{\rtf1\ansi\deff0 {\fonttbl {\f0 Courier New;}}\f0\fs60 " + rtf.ToString() + "}", true);
            data.SetData(DataFormats.Html, html, true);
            Clipboard.SetDataObject(data);
        }

Solution

  • Teams doesn't support rich HTML formatting like this, unfortunately. The best you'll get is some limited markdown support, but at least it's something. See here for more: https://support.microsoft.com/en-ie/office/use-markdown-formatting-in-teams-4d10bd65-55e2-4b2d-a1f3-2bebdcd2c772 .