简体   繁体   中英

Cannot copy html formatted string to clipboard

I am trying to copy html formatted string from my database to a word document. I need to be able to restore formatting like bullets. Using the below code, I get an exception

"The specified data type is unavailable."

Below is the code to copy to clipboard and write to word document.

DataObject clipDO = new DataObject();
clipDO.SetData(DataFormats.Html, HtmlClipboardData(temp));
Thread thread = new Thread(() => Clipboard.SetDataObject(clipDO, true));
thread.ApartmentState = ApartmentState.STA;
thread.Start();
thread.Join();
Microsoft.Office.Interop.Word.Paragraph oPara = wordApp.Selection.Paragraphs.Add(ref oMissing);
object pasteType = WdPasteDataType.wdPasteHTML;
oPara.Range.PasteSpecial(ref oMissing, ref oMissing, ref oMissing, ref oMissing, pasteType, ref oMissing, ref oMissing);

The code stopped working now. When it did work earlier, it would not copy html to clipboard. It would write to word doc, previous contents of clipboard. My helper method for adding header to the html string is below.

    private static string HtmlClipboardData(string html)
    {
        StringBuilder sb = new StringBuilder();
        Encoding encoding = Encoding.GetEncoding("utf-8");
        string Header = @"
        Version: 1.0
        StartHTML: {0:000000}
        EndHTML: {1:000000}
        StartFragment: {2:000000}
        EndFragment: {3:000000}
        ";
        string HtmlPrefix = @"
        !DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//TR""
        html
        head
        meta http-equiv=Content-Type content=""text/html; charset={0}""
        head
        body
        !--StartFragment--
        ";
        HtmlPrefix = string.Format(HtmlPrefix, encoding.WebName);

        string HtmlSuffix = @"
        <!--EndFragment-->
        </body>
        </html>
        ";

        // Get lengths of chunks
        int HeaderLength = encoding.GetByteCount(Header);
        HeaderLength -= 16; // extra formatting characters {0:000000}
        int PrefixLength = encoding.GetByteCount(HtmlPrefix);
        int HtmlLength = encoding.GetByteCount(html);
        int SuffixLength = encoding.GetByteCount(HtmlSuffix);

        // Determine locations of chunks
        int StartHtml = HeaderLength;
        int StartFragment = StartHtml + PrefixLength;
        int EndFragment = StartFragment + HtmlLength;
        int EndHtml = EndFragment + SuffixLength;

        // Build the data
        sb.AppendFormat(Header, StartHtml, EndHtml, StartFragment, EndFragment);
        sb.Append(HtmlPrefix);
        sb.Append(html);
        sb.Append(HtmlSuffix);
        return sb.ToString();
    }

The problem is that the html string is not copied to clipboard. When I have some special content(like content copied from command prompt) in clipboard already, PasteSpecial works and pastes that data into my word doc.

尝试改用DataFormats.Text。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM