Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to programmatically paste text into a specific Word document

How to programmatically paste text into a specific Word document

Scheduled Pinned Locked Moved C#
csharptutorialquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leif Simon Goodwin
    wrote on last edited by
    #1

    We need to bring a Word document into focus and then paste text into the document at the current cursor location. We know the name of the Word document. Unfortunately Word only has one instance and one main window, which means that I cannot simply locate the window, bring it into focusand paste the data. Is there a way to do this from C#?

    L M 2 Replies Last reply
    0
    • L Leif Simon Goodwin

      We need to bring a Word document into focus and then paste text into the document at the current cursor location. We know the name of the Word document. Unfortunately Word only has one instance and one main window, which means that I cannot simply locate the window, bring it into focusand paste the data. Is there a way to do this from C#?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I hate applications that steal focus, as many other users do. Do you need to show the Word-application, or would it suffice if you can embed Word in your application and push that to the foreground? Inserting text into a document would be rather simple, as a word-document is a zipped collection of files you can read with notepad. Do you want to replace text at a certain pre-determined position, or at the cursor? If the latter, you may be better of writing a Word-adding that doesn't steal focus. Many questions here :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      L 1 Reply Last reply
      0
      • L Leif Simon Goodwin

        We need to bring a Word document into focus and then paste text into the document at the current cursor location. We know the name of the Word document. Unfortunately Word only has one instance and one main window, which means that I cannot simply locate the window, bring it into focusand paste the data. Is there a way to do this from C#?

        M Offline
        M Offline
        Maciej Los
        wrote on last edited by
        #3

        [EDIT] I agree with [Eddy Vluggen](https://www.codeproject.com/script/Membership/View.aspx?mid=3984486) that you don't need to keep focus to the document/application to be able to paste piece od text. You need to refer to the document you want to use:

        document = InstanceOfWord.Documents["ShortDocumentName.docx"];
        document.Selection.PasteSpecial();

        document = InstanceOfWord.Documents("ShortDocumentName.docx")
        document.Selection.PasteSpecial()

        More at MSDN: [How to: Programmatically Insert Text into Word Documents](https://msdn.microsoft.com/en-us/library/6b9478cs.aspx) But, if a requirement is to keep focus, there's good news: there's buit-in method called: [Document.Activate Method (Word)](https://msdn.microsoft.com/en-us/vba/word-vba/articles/document-activate-method-word)

        InstanceOfWord.Documents["ShortDocumentName.docx"].Activate;

        InstanceOfWord.Documents("ShortDocumentName.docx").Activate

        1 Reply Last reply
        0
        • L Lost User

          I hate applications that steal focus, as many other users do. Do you need to show the Word-application, or would it suffice if you can embed Word in your application and push that to the foreground? Inserting text into a document would be rather simple, as a word-document is a zipped collection of files you can read with notepad. Do you want to replace text at a certain pre-determined position, or at the cursor? If the latter, you may be better of writing a Word-adding that doesn't steal focus. Many questions here :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

          L Offline
          L Offline
          Leif Simon Goodwin
          wrote on last edited by
          #4

          Thanks for the response. Our requirements are that the user must be able to configure our system such that data from a reader is pasted into a user chosen application. Because this is generic, the way we do it is to bring the app into focus and then paste the data. For images we use the clipboard. This is not intended to run on someone's work PC, rather it is an automated system, and giving an app focus is desirable as the operator can see the read data. We wish to post into the cursor position in the target document.

          L 1 Reply Last reply
          0
          • L Leif Simon Goodwin

            Thanks for the response. Our requirements are that the user must be able to configure our system such that data from a reader is pasted into a user chosen application. Because this is generic, the way we do it is to bring the app into focus and then paste the data. For images we use the clipboard. This is not intended to run on someone's work PC, rather it is an automated system, and giving an app focus is desirable as the operator can see the read data. We wish to post into the cursor position in the target document.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            In that case you would want to get a list of processes and enumerate its windows. You'd probably need some WinAPI to identify the correct window.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            L 1 Reply Last reply
            0
            • L Lost User

              In that case you would want to get a list of processes and enumerate its windows. You'd probably need some WinAPI to identify the correct window.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              L Offline
              L Offline
              Leif Simon Goodwin
              wrote on last edited by
              #6

              I already do that. Unfortunately Word only has one main window. I've tried to enumerate the child windows but that does not work, it only locates the topmost. If you run up two documents, then look in Task Manager, you'll see what I mean: one process and one window.

              L 1 Reply Last reply
              0
              • L Leif Simon Goodwin

                I already do that. Unfortunately Word only has one main window. I've tried to enumerate the child windows but that does not work, it only locates the topmost. If you run up two documents, then look in Task Manager, you'll see what I mean: one process and one window.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That's the only way I know to get the Windows that are created by the process.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups