none
Why can not use getdata and setdata after document copy RRS feed

  • Question

  • I want to copy all contents from word to clipboard and process then set back to clipboard

    use this code, got exeption when setdata or Clipboard.Clear()

    using System;
    using System.Windows.Forms;
    using Microsoft.Office.Tools.Ribbon;
    using HtmlAgilityPack;
    using System.Text.RegularExpressions;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Data;
    
                        Doc.Application.ActiveDocument.Content.Select();
                        Doc.Application.Selection.Copy();
                        Doc.Application.Selection.Collapse();
                        IDataObject clipData = Clipboard.GetDataObject();
                        String clipHtml = null;
                        if (clipData.GetDataPresent(DataFormats.Html))
                        {
                            clipHtml = (String)clipData.GetData(DataFormats.Html);
                        }
    
                        String html = "<h1>Hello</h1>";
                        Encoding enc = Encoding.UTF8;
                        string begin = "Version:0.9\r\nStartHTML:{0:000000}\r\nEndHTML:{1:000000}"
                          + "\r\nStartFragment:{2:000000}\r\nEndFragment:{3:000000}\r\n";
                        string html_begin = "<html>\r\n<head>\r\n"
                          + "<meta http-equiv=\"Content-Type\""
                          + " content=\"text/html; charset=" + enc.WebName + "\">\r\n"
                          + "<title>HTML clipboard</title>\r\n</head>\r\n<body>\r\n"
                          + "<!--StartFragment-->";
                        string html_end = "<!--EndFragment-->\r\n</body>\r\n</html>\r\n";
                        string begin_sample = String.Format(begin, 0, 0, 0, 0);
                        int count_begin = enc.GetByteCount(begin_sample);
                        int count_html_begin = enc.GetByteCount(html_begin);
                        int count_html = enc.GetByteCount(html);
                        int count_html_end = enc.GetByteCount(html_end);
                        string html_total = String.Format(
                          begin
                          , count_begin
                          , count_begin + count_html_begin + count_html + count_html_end
                          , count_begin + count_html_begin
                          , count_begin + count_html_begin + count_html
                          ) + html_begin + html + html_end;
                        DataObject obj = new DataObject();
                        obj.SetData(DataFormats.Html, new MemoryStream(
                          enc.GetBytes(html_total)));
                        Clipboard.SetDataObject(obj, true, 2, 100);

    If I comment out

    Clipboard.GetDataObject

    Then SetDataObject works well 

    But if keep it Clipboard.Clear() or Clipboard.SetDataObject got the same exception :

    ErrorCode: -2147221040

    Comment out

    Doc.Application.Selection.Copy();

    Clipboard.SetDataObject also works well.

    Why can not use Copy and GetDataObject then SetDataObject at the same time?

    I tried .Net 4 and .Net 4.6.1 got the same.
    • Edited by RickyWu1113 Saturday, March 28, 2020 3:59 AM
    Saturday, March 28, 2020 3:54 AM

All replies