Search code examples
c#visual-studio-2012ms-wordms-officeoffice-interop

Paste DataObject into Word C#


I'm trying to paste data from the clipboard into word, not just the text. I've managed to do an RTF text paste but I also need pictures/links/etc. The data I'm copying is from a website, and the program needs to paste it exactly as if I'm hitting CTRL-V into the document.

From what I understand, the clipboard will hold a DataObject, and I can't seem to find a way to get that into a word document.

I don't care if doc or docx is used. Either is fine.


Solution

  • Found the solution using Microsoft.Office.Interop.Word:

                        object oMissing = System.Reflection.Missing.Value;
                        object oEndOfDoc = "\\endofdoc";
                        Word._Application oWord;
                        Word._Document oDoc;
                        oWord = new Word.Application();
                        oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing);
                        Word.Paragraph oPara1;
                        oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
                        oPara1.Range.Paste();
                        oDoc.SaveAs(docOutput);