Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open an EXE, new problem

Status
Not open for further replies.

winniepough

Programmer
Feb 19, 2002
14
US
Option Explicit
Private Executable As String

Public Sub Form_Load()
'--------------------------------
Call Launch
'--------------------------------
End Sub
Public Sub Launch()
'--------------------------------
Executable = "E:\TC2000CD\TC2000.exe "

Call Shell(Executable, vbNormalFocus)
'--------------------------------
'IF THIS WAS ALL THE CODE THEN, THE FILE LOADS AND IS IN FOCUS
'WHEN THE NEXT INSTRUCTION IS EXECUTED, THE PROGRAM GOES TO THE TASK BAR AND IS DISPLAYED IN ORANGE
'THE REMAINDER OF THE CODE BELOW SEEMS TO NOT 'FOCUS' ON THE EXECUTABLE
'THE FOLLOWING, IF MANUALLY EXECUTED ON THE PROGRAM FUNCTIONS NORMALLY, TO DO WHAT I WANT THEM TO DO. Help!!!!
SendKeys "%(D)", False '"ALT + D", DataBase, pulldown menu
SendKeys "%(U)", False '"ALT + U", Update DataBase
SendKeys "%(U)", False '"ALT + U", Selects Internet vs. Dial - Up
'program Dial-Up later

While Timer < Now + &quot;00:01.00&quot; 'set up timer for DSL update
DoEvents 'definitely should finish in that time
Wend

SendKeys &quot;(ESC)&quot;, False 'Kill the Worden Notes pop-up
SendKeys &quot;(ESC)&quot;, False ' in case there are two 'pop-ups' to close
SendKeys &quot;%{F4}&quot;, False 'Send ALT+F4 to close
End Sub
 
Try using AppActivate statement before sending each key.
Like this:
--------------------------------------------------
AppID = Shell(Executable, vbNormalFocus)

AppActivate AppID
SendKeys &quot;%(D)&quot;, False '&quot;ALT + D&quot;
--------------------------------------------------

However Sendkeys and AppActivate have known for their problems. The best way to send keys to an application is using API.

Here is a sample code that uses keybd_event function:

MODULE SECTION
-----------------------------------------------------------
Public Declare Sub Sleep Lib &quot;kernel32&quot; (ByVal dwMilliseconds As Long)
Public Declare Sub keybd_event Lib &quot;user32&quot; (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Public Declare Function GetDesktopWindow Lib &quot;user32&quot; () As Long
Public Declare Function GetWindow Lib &quot;user32&quot; (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetClassName Lib &quot;user32&quot; Alias &quot;GetClassNameA&quot; (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function GetWindowText Lib &quot;user32&quot; Alias &quot;GetWindowTextA&quot; (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function PostMessage Lib &quot;user32&quot; Alias &quot;PostMessageA&quot; (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetForegroundWindow Lib &quot;user32&quot; (ByVal hwnd As Long) As Long
Function FindWin(AppTitle)

Dim WinClass As String * 252
Dim WinTitle As String * 252

FindWin = 0

AppTitle = UCase(AppTitle)

DesktopWin = GetDesktopWindow
FirstWin = GetWindow(DesktopWin, 5)

Do While FirstWin <> 0
ReturnClass = GetClassName(FirstWin, WinClass, 250)
ReturnVal = GetWindowText(FirstWin, WinTitle, 250)
WinTitle = UCase(WinTitle)
If Trim(WinTitle) = &quot;&quot; Then GoTo NextOne
If Left(WinTitle, Len(AppTitle)) = AppTitle Then
FindWin = FirstWin
Exit Function
End If
NextOne:
FirstWin = GetWindow(FirstWin, 2)
Loop


End Function
-----------------------------------------------------------

FORM SECTION
-----------------------------------------------------------
Private Sub Command1_Click()

Dim RunThis As String
RunThis = &quot;notepad.exe&quot;

AppCode = Shell(RunThis, vbNormalFocus)

wHandle = FindWin(&quot;Notepad&quot;)

SetForegroundWindow wHandle


keybd_event 72, 0, 0, 0 'send key &quot;h&quot;
keybd_event 69, 0, 0, 0 'send key &quot;e&quot;
keybd_event 76, 0, 0, 0 'send key &quot;l&quot;
keybd_event 76, 0, 0, 0 'send key &quot;l&quot;
keybd_event 79, 0, 0, 0 'send key &quot;o&quot;

End Sub
----------------------------------------------------------

Hope this helps,
 
Craigsander in:
Home > Forum Areas > Programmers > Languages > Visual Basic(Microsoft) -Version 5 & 6 Forum
How do i tell if an app is running?
thread222-236844
RE: May have provided the NEXT layer to my unwrapping the onion. I will give it a try.
As I fix one problem, another is uncovered. &quot;You never know what is under the next layer of the onion.&quot;
Adding delay Timer() and DoEvents forces a long fixed delay to prevent encountering the RunT ime error..
Response time is of the essence.


Niv3K in: Home > Forum Areas > Programmers > Languages > Visual Basic(Microsoft) -Version 5 & 6 Forum
Open an EXE, tiny new problem, closing the EXE
thread222-227807
provided the major breakthrough which uncovered the above layer.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top