Retrieve text of focused control in another application, namely IE

I want to write a function to retrieve the text of the active (focused) control, even if the control belongs to another application.

The function below does that in most cases.  However, it doesn't work if Internet Explorer is the foreground application and the user is in, for example, a TEXTAREA field.  (Like the one that I am typing in right now.)  Does that control not respond to the WM_GetText message or do you think GetFocus() is returning the wrong handle?  In either case, how would I go about reading the text from this type of control within IE?

function GetActiveText: String;
  MaxTextLen = 5000;
  FW, FC : HWND;
  Buf : array[0..MaxTextLen] of Char;
  FW := 0;
  FC := 0;
  FW := GetForegroundWindow;
  ThID := GetWindowThreadProcessId(FW,0);
    FC  := GetFocus();
  Result := Buf;

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not sure specifically why your code does not work. However, I use the following to get text from another app's edit boxes:

function GetWindowTextEx(win : HWND) : string;
      s : array [1..513] of char;
      i : integer;
      res : DWORD;
      Result := '';
      SendMessageTimeout(win, WM_GETTEXT, 512, Integer(@s[1]), 0, 1000, res);
      i := 1;
      while (i < 513) and (s[i] <> #0) do begin
            Result := Result + s[i];

It could be improved but it works.

Geoff M.
roknjohnAuthor Commented:

can you get your method to read text from an IE TextArea control?  For example, the control that I am typing this message into?
Russell LibbySoftware Engineer, Advisory Commented:

Won't work, because they are not "true" window controls (they don't have an hwnd). Without getting into the document object model for IE (IHTMLDocument2), you are probably going to be hard pressed to come up with a generic method that works in all cases.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

You're right. That's the first time I've come across a program in which that didn't work. It does work in the address bar though.

Geoff M.
roknjohnAuthor Commented:
I found this:

Peeking into Password Edit Controls & Internet Explorer - Super Password Spy++

It seems to focus on password fields, but I think I can extract the information I need from this.  Now, if I can only find the time. :-)
Russell LibbySoftware Engineer, Advisory Commented:

Yes, saw that one two....
But like i said, you need to delve into the document interface in order to get this information. This will work fine if you only intend on dealing with IE though... (won't work with anything else ;-)

roknjohnAuthor Commented:
Hmm, looks more complicated than its worth right now.

My plan was to put together a freeware spell checker that would work across applications.  But, I think I'll just google for "Spell Checker" instead of "IHTMLDocument2  delphi"

Every heard of "As-U-Type"?
roknjohnAuthor Commented:
I think I found a real gem! The trail version works cool as heck!  It works on IE & Outlook, just like I wanted to do.  Only $29.95 (US) and I don't have to learn IHTMLDocument2!!  

PS. You can disable it for certain apps, like Delphi.  

Thanks Russell, your information solved my curousity.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.