Copying Selected Text From an External Program

mithrandiir42

Member
Joined
Mar 7, 2006
Messages
16
Programming Experience
3-5
Hi everyone,
I've been been beating my head against the wall trying to get at the selected text of an external program. Basically my program will run in the background and wait for a hotkey to be pressed. When that happenes, I want to retrieve the selected text from whatever program i'm working in.

My first approach was to simulate keystrokes to simulate a ctrl+c which I suppose is still a possibility but it seems like there should be a much cleaner way to do this by using SendMessage to the active text box or something like that. I'm also want to steer away from using keystrokes because i'm worried it might interfere with what the user is doing.

If anyone knows of a good way to go about this, I'd really appreciate any help.

Thanks a lot in advance!

-Jeremy
 
Ah yeah, that was a little funny. i forgot to declare the type. anyway, i changed it to the code below but it actually errors before that point. It errors out on the SetDataObject line that i have highlighted in red. and says "The Requested Clipboard Operation Failed"

VB.NET:
[COLOR="Red"]Clipboard.SetDataObject(data_object)[/COLOR]
Dim data_test As IDataObject = Clipboard.GetDataObject()

I'm baffled as to why it works with Html or Rtf but crashed when trying to set a clipboard of plain text back. *shrug*
 
ok, never heard of that one before, it could happen if another process is accessing the clipboard at the same time. .Net 2.0 has got a method overload fix for this with retry parameters, see http://msdn2.microsoft.com/en-us/library/ms158293(VS.80).aspx

For .Net 1.x you just have think up some clever workaround.
 
Back
Top