How to programmatically paste text into a specific Word document
-
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#?
-
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#?
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[^]
-
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#?
[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
-
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[^]
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.
-
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.
-
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.
-
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.