title: VBA SendKeys to specific window
body: <p>I have a spreadsheet that summarizes usage of a server.  The spreadsheet references a text file that is updated when I have VBA send some commands to the Linux server through the Windows command prompt (and Cygwin).  My problem is that the only way I have found to execute commands in Cygwin is through the <code>SendKeys</code> command.  This results in two problems:</p>
  
  <ol>
  <li>I have to trust that the user will not click somewhere until the script is done running.</li>
  <li>I have to hardcode the amount of time to wait for the command to be executed before the next command can be sent.</li>
  </ol>
  
  <p>I would (and did) try creating a bash file to run in Cygwin, but I don't know of a way to do this that also allows the user to enter their password to log into the Linux server.  I am currently getting the user's password from a UserForm that is run before the main code is executed.  This password is then saved to a "very hidden" worksheet that is deleted when the application is closed.</p>
  
  <p>I am currently using the following script to run the commands:</p>
  
  <pre><code>Public Sub test()
      ' Declare Variables
      Dim cmd As String
      Dim ret As Double
      Dim LastRow As Integer
      Dim PssWrd As String
  
      PssWrd = Worksheets("PssWrd").Range("A1").Value
  
      ' Run Linux Commands
      cmd = "C:\cygwin\Cygwin.bat"
      ret = Shell(cmd, vbMinimizedFocus)
      wait 0.02
      cmd = "ssh " &amp; Worksheets("Settings").Range("LOGIN") &amp; "~" ' Format: user@hostname
      SendKeys cmd
      cmd = PssWrd &amp; "~"
      SendKeys cmd
      wait 2.78
      cmd = "{(} date ; fs ; qstat ; date {)} &gt; &amp; zout &amp;~"
      SendKeys cmd
      cmd = "exit~"
      SendKeys cmd
      wait 0.5
      cmd = "exit~"
      SendKeys cmd
      wait 5
      ' Update PivotTable and other data
  End Sub
  Public Sub wait(PauseTime As Double)
      Dim Start As Double
      Dim Finish As Double
      Dim TotalTime As Double
      Start = Timer    ' Set start time
      Do While Timer &lt; Start + PauseTime
          DoEvents    ' Yield to other processes
      Loop
      Finish = Timer    ' Set end time
      TotalTime = Finish - Start    ' Calculate total time
  End Sub
  </code></pre>
  
  <p>Is there any way to at least send the keystrokes to a specific window instead of just the active window?  Any help would be greatly appreciated, and any additional necessary information will be readily provided.</p>