Question Copying Text from word

jaiprakash

Member
Joined
Nov 28, 2008
Messages
9
Programming Experience
Beginner
I want to write a program that can copy a selected text in a active program (like word, excel,...) then paste it in my program to do some editting.

Is it feasable, can anyone give me a clue how can I do it? :confused:
 
No i dont know about Clipboard.SetText() methods.. Ill go through those methods.. Thanks for the Quick response.. Please can i have some examples on this methods?
 
Last edited:
If you select the text in the word document and copy it (Ctrl+C), then you could run this.
VB.NET:
Dim ClipboardText As String
If Clipboard.ContainsText() Then
    ClipboardText = Clipboard.GetText()
End If
Clipboard.SetText("You copied """ & ClipboardText & """ from the text file."
Now if you paste it, it would show up with the added text.

If you want to copy the text without having to do it manually, look into StreamReader/StreamWriter.
 
hi

hi i got the answer..

using this we can get the selected Lines into a string


VB.NET:
 Dim document As Word.Document = Globals.ThisAddIn.Application.ActiveDocument

        'Let it speak!
        Dim jai As String = document.ActiveWindow.Selection.Text
 
Hi Jai, you seem to have already found the answer to your problem but I'm still curious about it because I dont really see the reason for your original request. You want to automate the copying & pasting from Word & Excel. A user will open up a file in either program, select the text they want to work with, switch over to your VB program, click an event and the text will appear in the VB app to work with.

If a user is already doing that much hands on work such as mentioned above, I dont see why they cant just copy the text and pasted it into VB rather then coding to automate both Word & Excel processes just to copy text that is already selected. There are different options available to read data from both Word & Excel files without the need of automating either program and there are also options to add Word & Excel functionality directly into your vb program itself.

With that said, if you do continue to work with office automation I found a cheat method I use that helps as a starting point at least when im stuck on syntax. In your msoffice program goto tools on the menu, click Macros then Record a new macro. Then perform your task in the Word or Excel program and stop it when your done. After that go back into the Macros menu and choose Edit Macro. The VBA editor will then open up and show you the coding it generated to perform the task you recorded. There may still be syntax diffences between what you see there & what you need to actually put in your VB app but you have something to work with and to look up now.
 
Back
Top