Microsoft KB Archive/259103

= How to use the Windows Script Host to automate an application =

Article ID: 259103

Article Last Modified on 8/27/2007

-

APPLIES TO


 * Microsoft Visual FoxPro 3.0 Standard Edition
 * Microsoft Visual FoxPro 3.0b Standard Edition
 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q259103



SUMMARY
The Microsoft Windows Script Host is a language-independent scripting host for 32-bit Windows operating system platforms. Windows Script Host is integrated into Microsoft Windows 98, Windows ME, Microsoft Windows 2000, Windows XP, Windows Server 2003, and Microsoft Internet Information Server 4.0. It is included with Microsoft Windows NT 4 Option Pack. To download the Windows Script Host, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms950396.aspx



MORE INFORMATION
This article describes how to use the Windows Script Host to run, activate, and send keystrokes to applications.

The Windows Script Host Object Model includes a versatile Run method that allows programmers to specify window states of the applications they run. The AppActivate method allows programmers to activate applications by passing the target application's windows caption. The SendKeys method allows programmers to send keystrokes to the activated application. Use the following code: *Run Winword, give it a string and sort it and then run Notepad and paste in. Declare Sleep IN WIN32API INTEGER &&wait for milliseconds-may not be necessary in all instances WshShell = CreateObject(&quot;wscript.Shell&quot;) _Cliptext=&quot;One&quot;+VFPCR+&quot;Two&quot;+VFPCR+&quot;Three&quot;+VFPCR+&quot;Four&quot;+VFPCR+&quot;Five&quot;+VFPCR WshShell.Run( &quot;WinWord&quot;, WSNORMAL)    && Run normal =Sleep(1000) WshShell.SendKeys( &quot;^v&quot;)      &&Ctrl++v =Sleep(100) WshShell.SendKeys( &quot;%as&quot;)     &&Alt+a+s =Sleep(100) WshShell.SendKeys( &quot;{ENTER}&quot;) &&Enter =Sleep(100) WshShell.SendKeys( &quot;^a&quot;)      &&ctrl+a =Sleep(100) WshShell.SendKeys( &quot;^c&quot;)      &&ctrl+c =Sleep(100) WshShell.SendKeys( &quot;%{F4}&quot;)   &&Alt+F4 =Sleep(100) WshShell.SendKeys( &quot;n&quot;)       && n-(No) =Sleep(100) WshShell.Run(&quot;notepad&quot;,1) =Sleep(100) WshShell.AppActivate(&quot;notepad&quot;) =Sleep(1000) WshShell.SendKeys( &quot;^v&quot;)      &&Ctrl+V  Sorted!
 * 1) Define VFPCR  chr(13)+chr(10)
 * Constants for window state when using run method
 * 1) Define WSHIDE         0 && Hides the window and activates another window.
 * 2) Define WSNORMAL       1 && Activates and displays window.
 * 3) Define WSMIN          2 && Activates the window and displays it as a minimized window.
 * 4) Define WSMAX          3 && Activates the window and displays it as a maximized window.
 * 5) Define WSLAST         4 && Displays a window in its most recent size and position. The active window remains active.
 * 6) Define WSSAME         5 && Activates the window and displays it in its current size and position.
 * 7) Define WSMINNEXT      6 && Minimizes the specified window and activates the next top-level window in the Z order.
 * 8) Define WSMINSAME      7 && Displays the window as a minimized window. The active window remains active.
 * 9) Define WSSAMECUR      8 && Displays the window in its current state. The active window remains active.
 * 10) Define WSORIG         9 && Activates and displays the window.
 * 11) Define WSSETSHOW     10 && Sets the show state based on the state of the program that started the application.

