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, tiny new problem, closing the EXE 1

Status
Not open for further replies.

winniepough

Programmer
Feb 19, 2002
14
US
See the three lines following the text in RED, thanks, Jim

Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtrInfo As Long)
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" _
(ByVal hwnd As Long, ByVal lpClassName As String, byvalMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwbd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Dim Executable
Public Sub Form_Load()
'--------------------------------
Call Launch

'--------------------------------
End Sub

Private Sub Launch()
Dim RunThis As String
Dim wHandle As Long
RunThis = "E:\TC2000CD\TC2000.exe "
Executable = Shell(RunThis, vbMaximizedFocus)
wHandle = FindWin("TC2000")
SetForegroundWindow wHandle

Sleep 5000 'allow time to load, required to function

SendKeys "%D", True 'alt D to select DataBase pull down
SendKeys "U", True 'U to select Update, using the Internet, vs. telephone 800 #
SendKeys "%U", True 'alt U to initiate Actual update 'Now'

Dim LTime
LTime = Timer()
While Timer() - LTime < 10 '10 seconds delay
DoEvents
Wend

SetForegroundWindow wHandle
NEXT THREE LINES DO NOT WORK, BUT EQUIVALENT FUNCTIONS MANUALLY!!
I TRIED THE keybd_event WITH VK_xxxx, IN MANY COMBINATIONS ALSO, NO LUCK

SendKeys &quot;&H1B&quot;, True '(ESC), kill the Worden otes
SendKeys &quot;&H1B&quot;, True '(ESC), kill any secondary pop-up
SendKeys &quot;%&H73&quot;, False 'alt+F4, i.e. &quot;%(F4)&quot; close the &quot;TC2000&quot; program

Unload Form1 'FORM DOES UNLOAD, BUT &quot;TC2000&quot; IS STILL OPEN [/red]
End Sub

Function FindWin(AppTitle)
Dim FirstWin As Long
Dim DesktopWin As Long
Dim ReturnClass As Long
Dim WinClass As String * 252
Dim ReturnVal As Long
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
 
All you need to do is put the following code behind a command button

CCDoors.Close - Closes an open DB
CCPrint.Close - Closes DB
Set CCDoors = Nothing - Frees up the memory
Set CCPrint = Nothing - Frees up the memory
Unload Me - unloads the form
End - exits and closes all remaining windows etc..


This works for me in the program I wrote. &quot;The beauty of the second amendment is, that it will not be needed until
they try to take it.&quot; - Thomas Jefferson
 
have you tried

SendKeys &quot;&H1B&quot;, True '(ESC), kill the Worden otes
SendKeys &quot;&H1B&quot;, True '(ESC), kill any secondary pop-up
SendKeys &quot;%&H73&quot;, True 'alt+F4, i.e. &quot;%(F4)&quot; close the &quot;TC2000&quot; program
 
Neither of the above two suggestions seem to work.

Althought this is a data base, I do not query or change any data through this Call. I only ask for the supplier of the data to download new data and they update the database. Then, the EXE closes the internet access. But I do need to close the EXE.
Then, with another program, I query, etc. So I never in this data update program, i.e. the EXE, do I need to close the queries, et. al.

I had tried the 'True&quot; as suggested, but nothing seems to work after the database is updated from the net. It appears that I have lost Focus at the point that the program fails.

It had been suggested that I cannot Close the EXE because I activated via the Shell command rather that a Create Object. I have tried the Create Object, both as linked and embedded, neither of those allows me to do the basic update that I have succeded to do with the Shell. I can load the EXE, but was unable to get a response by the SendKey methods with the Object.

 
Niv3K's suggestion worksin closing the EXE, however, I now occasionally get a RunTime automation error which i cannot kill by automation. So I still have the problem that I can not fully automate the process. I do not care if the process fails, as I have other code that determines the age of the data. If it is old, ( and I have the checks to be able to tell ) I simply try again after a short wait until the server that I am calling is available. So I need to know how to kill the msgbox that identifies the fact that I have a run time error. I can kill the the msgbox manually, but that is not within the scope of full automation.
It is like peeling an onion. You never know what the next layer will bring or if there is a next layer. Thanks to Niv3k and the rest of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top