none
Range.PasteSpecial method gives error RRS feed

  • Question

  • Hi, I want to open word document with HTML content(in winForm C#). I am using the following code but PasteSpecial method gives error: Command Failed!

    Here is my code:

    object missing = System.Reflection.Missing.Value;
                object Visible=true;
                object start1 = 0;
                object end1 = 0;
     
                ApplicationClass WordApp = new ApplicationClass();
                Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
                Range rng = adoc.Range(ref start1, ref missing);
     
                try
                {
                    DataObject cbObj = new DataObject();
                    cbObj.SetData(DataFormats.Html, "<html><body><i>HTML CONTENT</i></body></html>");
                    Clipboard.SetDataObject(cbObj, true);
    
                    object DataType = Microsoft.Office.Interop.Word.WdPasteDataType.wdPasteHTML;
                    rng.PasteSpecial(ref missing, ref missing, ref missing,
                        ref missing, ref DataType, ref missing, ref missing);
                    
                    WordApp.Visible = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }        

     
         



    • Edited by endhck Thursday, August 15, 2013 8:58 AM
    Thursday, August 15, 2013 8:57 AM

Answers

  • Hi Endhck, Html content in clipboard should follow HTML Clipboard Format(http://msdn.microsoft.com/en-us/library/aa767917%28v=vs.85%29.aspx). Here is sample for your reference:

    object missing = System.Reflection.Missing.Value;
    object Visible = true;
    object start1 = 0;
    object end1 = 0;
    
    Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
    Word.Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
    Word.Range rng = adoc.Range(ref start1, ref missing);
    
    try
    {
    
          DataObject cbObj = new DataObject();
          cbObj.SetData(DataFormats.Html, @"Version:1.0
    StartHTML:000000151
    EndHTML:000000312
    StartFragment:000000260
    EndFragment:000000280
    StartSelection:000000260
    EndSelection:000000280
    <!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">
    <HTML><HEAD></HEAD><BODY><!--StartFragment--><I>HTML CONTENT</I> <!--EndFragment--></BODY></HTML>");
    
          Clipboard.SetDataObject(cbObj, true);
    
          object DataType = Microsoft.Office.Interop.Word.WdPasteDataType.wdPasteHTML;
          rng.PasteSpecial(ref missing, ref missing, ref missing, ref missing,ref DataType, ref missing, ref missing);
    
          adoc.SaveAs2(@"E:\Test\Output.docx");
    }
    catch (Exception ex)
    {
          MessageBox.Show(ex.Message);
    }
    
    WordApp.Quit();
    Hope it can help you.

    Saturday, August 17, 2013 12:07 PM