Xaphann
Xaphann

Reputation: 3677

Programatically paste text into text box in another application

I want to be able to paste the results from my application to another application. Been Googling and all I can find is the ApplicationCommands.Paste. That will paste it into my application not another application.

Background: My application needs to interact with a very old application. This application is not being developed and DOES NOT have any API calls. Meaning any answers like "Link your application via DLLs" will only be excepted if it also includes a time machine :P So the solution is to simple paste results from my app to the old application.

For workflow reasons the client wants my Application to disappear and automatically paste the results into an open text box in (if it fails then manually pasting the results in).

Hiding the window is simple, but how do I find current active application and then paste it in? Is this even possible? Also note this application is NOT .NET (VB 6 if I am correct).

Upvotes: 1

Views: 4846

Answers (1)

Jcl
Jcl

Reputation: 28272

You can bring up the other app and send a key combination... something like:

[DllImport ("User32.dll")]
static extern int SetForegroundWindow(IntPtr hwnd);

public static void PasteToApplication(string appName)
{
  var proc = Process.GetProcessesByName(appName).FirstOrDefault();
  if(proc != null)
  {
    var handle = proc.MainWindowHandle;
    SetForegroundWindow(handle);
    SendKeys.SendWait("^v");
  }
}

This should bring up the other app's window and send a ctrl-v command. With a bit of experimentation, you could find out the exact handle of the control you want to send the paste to, and set focus on it aswell

Upvotes: 3

Related Questions