How can I read selected text in another application, by vb program?

  • Thread starter Thread starter ptpwjp
  • Start date Start date
P

ptpwjp

How can I read selected text in another application, by vb program?

I want read from vb program
same text selected (for example by mouse) in another application (for
example IE, or Word ).
I want to have this text in any string variable, or in any file.
I don't know how many IE or Words or another applications will be opened.
Somebody can selected texts in several application,
in this situation I want to read only last selected text.
How to do it?
 
ptpwjp said:
How can I read selected text in another application, by vb program?

I want read from vb program
same text selected (for example by mouse) in another application (for
example IE, or Word ).
I want to have this text in any string variable, or in any file.
I don't know how many IE or Words or another applications will be opened.
Somebody can selected texts in several application,
in this situation I want to read only last selected text.
How to do it?

The newsgroups you have listed are mutually exclusive. Do you want a VB
6 recommendation, or a vb.net recommendation? The vb.general.discussion
group would be for a VB 6 question while the dotnet.languages.vb group
would be for .Net.

After deciding your platform, you need to clarify how this would work.
Do you want a user in your code to initiate a copy of the selected data
somehow? Do you want any selected text in any app captured by your app?
 
You posted to two different groups that cover very different versions of VB.
The earlier versions(VB6 or lower) are not based on dotnet and they don't
have "dotnet" in the group name. The newer version are all based on dotnet
which started with VB 2002, and they have separate groups that either have
"dotnet" or "vsnet" in the group name.

There are many things broken between VB6(Released in 1998), and VB 2002+.
VB6 code almost always produce compilation errors in VB.Net, and may not
work as expected, and usually require a rewrite to make it work properly.

Please repost your question again and include what VB version you are using.

<Question ignored>
 
The newsgroups you have listed are mutually exclusive. Do you want a VB
6 recommendation, or a vb.net recommendation? The vb.general.discussion
group would be for a VB 6 question while the dotnet.languages.vb group
would be for .Net.
Visual Basic 2008 Express Edition

After deciding your platform, you need to clarify how this would work. Do
you want a user in your code to initiate a copy of the selected data
somehow?
I want to transform text selected by user ,
(only if he will want to -only if he push bottom at my application),
and write new version of text to file.
 
ptpwjp said:
Visual Basic 2008 Express Edition

Please repost your question in new thread, or remove
microsoft.public.vb.general.discussion from your reply.

Thank you
 
ptpwjp said:
Visual Basic 2008 Express Edition


I want to transform text selected by user ,
(only if he will want to -only if he push bottom at my application),
and write new version of text to file.

You are going to call:
SendMessage(hwndChild, Win32.User.EM_GETSELTEXT, 0, builder)

builder is a stringbuilder object to receive the text. hwndChild is the
hwnd to the control to copy the text. Look at
http://www.pinvoke.net/default.aspx/coredll/SendMessage.html to see how
you declare and call this windows function.

You would need to find the hwndChild by first identifying the main
window of the external process. The System.Diagnostic.Process class
will help you find the main window handle to all other processes. You
could copy any text from all of the processes, or you could identify the
process somehow and use the SendMessage function with that hwnd.

BTW, I've dropped the vb6 group from this reply.
 
Thank you very much,
You would need to find the hwndChild by first identifying the main window
of the external process. The System.Diagnostic.Process class will help
you find the main window handle to all other processes. You could copy
any text from all of the processes, or you could identify the process
somehow and use the SendMessage function with that hwnd.
but , which of a lot of process is the last process?
I send ALT + TAB from my application, to be in last process,
but in this situation my application lost processor, and don't do next
instruction :).
 

Because you have targeted two distinctly different languages. Specific
answers for one language generally do not apply to the other.

This dotnet group is the more apropreate place for your question. Now
that you know where to ask your .Net questions, you do not need to include
the general discussion group which discusses VB6 and prior versions.

Starting a new thread would allow you to remove the un-needed group....
 
|
| >> Visual Basic 2008 Express Edition
| >
| > Please repost your question in new thread, or remove
| > microsoft.public.vb.general.discussion from your reply.
| Why?

Because we fncking said so, Bill
 
ptpwjp said:

Because your question is about VB.Net and the comp.lang.basic.visual.misc
group is NOT about VB.Net. It is a Classic VB group, which deals with VB6
and earlier versions, and which is completely different from VB.Net. Do you
have a problem with that? Tell you what, if you want to post your stuff to
irrelevant groups then let's add a few more . . . here goes . . .

Mike
 
Visual Basic 2008 Express Edition

Because you posted your question in two groups
that are for completely different things. VB.Net
is covered by microsoft.public.dotnet.languages.vb.
The other group is for regular VB. (VB6 and lower.)

You're using VB.Net.

The two VBs have very little in common. It's
confusing because they're both often just called "VB",
but the answer to your question would be different in
each.
It's made even more confusing because Microsoft
has created a hodge podge of different names and
versions. VB.Net has a lot of names:
VB7. 8, 9. VB 1, 1.1, 2, 3, 3.5. VB 2003, 2005, 2008.
Those are different versioning systems based on
whether one is referring to the language, the framework
version, or the Visual Studio version. But they're all
VB.Net!

Regular VB (mostly VB 5/6) is a product that produces
compiled software and is COM-centric. VB.Net is Java-like,
producing JIT-compiled "assemblies", using the
..Net Framework, which is similar to the Java VM.

So the only thing shared by the two VBs, besides the
name, is a vague similarity in the appearance of their
code.
 
ptpwjp said:
Thank you very much,

but , which of a lot of process is the last process?
I send ALT + TAB from my application, to be in last process,
but in this situation my application lost processor, and don't do next
instruction :).


I was afraid that would be your next question :(.

I'm not sure how the alt-tab list of processes works.
 
What you want to do is very complex and
may not be possible. Presumably you want
to find the foreground window to get the
last selection. With Active Accessibility you
could track changes in focus and use that to
get the foreground window. Then maybe you'd
get the selection from the last window that
was in the foreground? Already it's getting
awkward.

Assuming that you can get the window you want,
then you're faced with a different scenario for each
program.

Word has an object model.

For IE you want
to get the document.selection IF the document.
selection.type is "Text". So you need to get hold of
the IE document probably by enumerating
child windows until you find an Internet Explorer_Server
class window that you can retrieve a Document object
from. You *could* do that, but what if the person is
using Firefox?

For Notepad you might
be able to use EM_GETSEL along with API to get
window text. I'm not sure about that. But even if it
works that's only for a standard edit control.

EM_GETSELTEXT is only for a RichEdit and probably
won't work for a foreign process. Functions that return
a pointer won't return a valid pointer to an external
process because the pointer is a relative address.

Etc.

There's nothing that says
a program must make it's text or selection available.
Even Active Accessibility has limits as to what it can
return from different types of windows, and as far as
I know there's no AA method to return the current
selection -- only the text content.

And even if you work all that out, how do you figure out
what the last selection's parent program is? Will you
create a reference list of unique window class names
and then just drill down until you find one? .... Then
decide from there how to get the selection? So you'd
retrieve the document object if you come across an
"Internet Explorer_Server" class window, or drill down to
the edit window when you find a "Notepad" class window,
etc.? With that method you'll be severely limited in
terms of how many program types you can deal with.
You'll get IE but not Firefox or Opera; MS Word but not
OpenOffice Writer, Abiword, WordPro, etc.

Maybe someone will come up with a way to do what
you want. Maybe there's some kind of high-level
functionality that I'm not thinking of. But I'd be surprised
if that's the case. It sounds like you're trying to
just send key sequences to get the different windows,
but I don't see how that can work. You can't just usurp
the Desktop and set focus to whatever window you like.
 
What you want to do is very complex and
may not be possible. Presumably you want
to find the foreground window to get the
last selection. Yes.

......
Word has an object model.

For IE you want
to get the document.selection ...... but what if the person is
using Firefox?
It is reason why I send ctrl+ins , it is universal method I think.
But I can't get data to variable in my program. Haw can I do it?
And even if you work all that out,
I don't understand this, I'm not English.
how do you figure out
what the last selection's parent program is?
It isn't interesting for me what parent program is.
I need only selected text.

In old times, sometimes, in newspapers there was same programs which added
new menu to system menu. For example: "change to yellow", near menu copy,
past, cut, delete. But it was always difficult to uninstall, and I don't
like this kind of changes. But the sense of problem is the same.


......
 
It is reason why I send ctrl+ins , it is universal method I think.
But I can't get data to variable in my program. Haw can I do it?

That's interesting. I didn't know about that.
I just tried the API keybd_event, sending the
following parameters in succession:

17, 0, 0, 0
45, 0, 0, 0
45, 0, KEYEVENTF_KEYUP, 0
17, 0, KEYEVENTF_KEYUP, 0

It seems to work to get the current selection in
the active window. I can then just retrieve the
clipboard text. I'm running on Win98SE so I don't
know whether there might be security issues on
later systems with automated keystrokes.

Also, if they click a button in your program window
then of course they've changed the active window.
Even if you track changes in the active window, if
you want to catch the selection with keystrokes
then you will always be too late. Once you are
informed of the focus change you can no longer use
keystrokes.

Have you thought of trying a systemwide keyboard
hook? I'm not sure if this would work, but what I'm
thinking is something like the following:


* You set a keyboard hook.

* The person using your program uses a unique key
combination when they want to select current text.

* Your hook watches for that combination and when
it arrives you use the keybd_event method to capture
the current selection.

I don't know the specifics of how to do that in .Net.
Maybe someone else can provide that. And you'll
probably need to write a C++ DLL -- or obtain one --
to get your keyboard hook.
 
I'm afraid my post is not getting through to
the MS servers, though it may be getting
through to Google groups. (there's been a lot
of trouble that way lately.)

Here's another try:
It is reason why I send ctrl+ins , it is universal method I think.
But I can't get data to variable in my program. Haw can I do it?

That's interesting. I didn't know about that.
I just tried the API keybd_event, sending the
following parameters in succession:

17, 0, 0, 0

45, 0, 0, 0

45, 0, KEYEVENTF_KEYUP, 0

17, 0, KEYEVENTF_KEYUP, 0

It seems to work to get the current selection in
the active window. I can then just retrieve the
clipboard text. I'm running on Win98SE so I don't
know whether there might be security issues on
later systems with automated keystrokes.

Also, if they click a button in your program window
then of course they've changed the active window.
Even if you track changes in the active window, if
you want to catch the selection with keystrokes
then you will always be too late. Once you are
informed of the focus change you can no longer use
keystrokes.

Have you thought of trying a systemwide keyboard
hook? I'm not sure if this would work, but what I'm
thinking is something like the following:


- You set a keyboard hook.

- The person using your program uses a unique key
combination when they want to select current text.

- Your hook watches for that combination and when
it arrives you use the keybd_event method to capture
the current selection.

I don't know the specifics of how to do that in .Net.
Maybe someone else can provide that. And you'll
probably need to write a C++ DLL -- or obtain one --
to get your keyboard hook.
 
Back
Top