locked
Using C# Programatically paste from the clip board into a Word document RRS feed

  • Question

  • Can anyone tell me how to Programatically paste the clipboard contents to a word document using C#?? 

     I am very new to programming (2 months). I am using Visual Studio.NET and C#. I have made an Add-In for Word. One of the controls in my Add-In Programatically copies a textbox (richTextBox1) contents to the Clipboard. I used the following code:

    Clipboard.SetText(richTextBox1.Text);

    Then if I right click and select Paste the contents are pasted onto the Word document.

    However,  I would like to Programatically paste the clipboard contents to the word document? Can anyone tell me how to Programatically paste the clipboard contents to a word document using C#?? 

    ( P.S. I searched. However there is no Clipboard.paste method. I did find an Interface in the Object
    Browser at  Microsoft.Office.Interop.Word named "Selection" which has a method named "void Paste()". However when I derived a class from it and then instantiated the class I still did not have access to the "Paste()" method).

    • Moved by jack 321 Monday, June 9, 2008 4:32 AM off topic for C# general
    Wednesday, June 4, 2008 10:02 PM

Answers

All replies

  • As I discuss on my blog entitled, Tribal Knowledge: Working with Office Interops, one way to divine the inner workings of any office document and how to manage it via the interops is to record a macro of the process needed. Once done examine the vba code, it will show settings changes and other items of interest that can lead the way through the tribal knowledge of the interops. Most the object calls are the same under the covers...good luck.

    Otherwise this question won't get much action in the C# group...try posting it in the Discussions in Office Development or for a better responses or Discussions in Automation forum for interop questions.

    William Wegerson www.OmegaCoder.Com
    Thursday, June 5, 2008 10:39 AM
  • Thank you for your response. I took your advice concerning the Macros. However, the Macro shows the code in VB. I need the code in C#. the VB code for "paste" is "selection.paste". Do you know what it is in C#??

    I want to make a correction to the P.S. section of my original post:   I WAS able to instantiate a class using the "Interface" in the Object Browser at  Microsoft.Office.Interop.Word named "Selection" which contains a method named "void Paste()".

    However when I derived a class from it and then instantiated the class and called the "Paste()" method it threw an error because there is no code in the paste method. Can you give me an example of code that I can put into the past method??

    namespace WordAddIn2
    {
        class Class2:Selection        //Implements the Interface found at Microsoft.Office.Interop.Word.Selection
        {
            #region Selection Members...

     
    public void Paste()                 // The paste method of the derived class named Class2
            {
              
                throw new NotImplementedException();
            }
    ..............................................................................................................................................................................................................


    Class2 y = new Class2();                 // In my Windows Form I have instantiated Class2 so that I can use the Paste method
                y.Paste();                             

    ................................................................................................................................................................................................................

    When I run the program it throws an exception because I have not Implemented the Paste Method. The problem is that I do not know how to Implement the Paste method. Does anyone know how?????

    Thursday, June 5, 2008 12:01 PM
  • This was moved to the Off Topic forum. I can move it to a specific forum for you, if you'd like.

    Thanks!


    Ed Price (a.k.a User Ed), SQL Server Experience Program Manager (Blog, Twitter, Wiki)

    Thursday, May 10, 2012 5:54 AM