VB6 If AppActive - lost

(code that errors)

If AppActivate("Example Program") >= 0 Then numbertxt.Text = numbertxt.Text - 1

I'm trying to simply make it If the (targeted "Example Program") program isnt running still *due to crash*, DON'T make the counter - 1

But it wont do that...

Compile Error: Expect function or variable.

(Below is the entire sub)

Private Sub Command1_Click()
Command1.Caption = "Stop"
Do
If GetAsyncKeyState(vbKeyRButton) < 0 Then
Command1.Caption = "Start"
retry.Enabled = True
GoTo suicide
End If
cont:
retry.Enabled = False
MoveMouseCursor doorx, doory
ButtonClick (vbLeftButton)
Pause (retry)
MoveMouseCursor passx, passy
ButtonClick (vbLeftButton)
Pause (retry)
AppActivate "Example Program" ', 1
'---
Clipboard.SetText (numbertxt.Text)
'---
Pause (retry)
MoveMouseCursor okayx, okayy
ButtonClick (vbLeftButton)
SendKeys ("^v")
MoveMouseCursor Text1, Text2
ButtonClick (vbLeftButton)
If AppActivate("Example Program") >= 0 Then numbertxt.Text = numbertxt.Text - 1
Pause (retry)
Loop
suicide:
End Sub


--Thanks
xentixAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
trkcorpConnect With a Mentor Commented:
You obviously did not run or understand the code.  It does not load multiple instances of calc.exe.  It loads one instance and stores the task ID in module level variable rtn.  First click loads calc.exe via rtn = Shell("calc.exe", vbNormalFocus) - then if you click the button again it looks at the "rtn" variable to see if it has value.  If it does then AppActivate will transfer control to the calc.exe that we loaded on the first click. (The program IS running)  If the value of rtn is not a valid task ID then an error (5:Invalid procedure call or argument) is generated. (The program (or task) is NOT running)  
This error will occur if the calc.exe we started on the first button click has been terminated and so AppActivate cannot transfer control to an object that does not exist thereby generating an error.  Because the code has an error trap instructing it to branch to tag "startnew" on error it goes to startnew and initiates a new calc.exe.
Make a new window, add Command1 button, paste this code into the code window.
Maybe If we code it like this you will get it:

Option Explicit
Dim rtn As Double

Private Sub Command1_Click()
On Error Resume Next
AppActivate rtn
If Err > 0 Then  
  'If the (targeted "Example Program") program isnt running still *due to crash*, DON'T make the counter - 1
  If MsgBox("No calc.exe is running. Start calc.exe?", vbQuestion + vbOKCancel, "No Task") = vbOK Then
    rtn = Shell("calc.exe", vbNormalFocus)
    AppActivate rtn
  End If
Else
  'If the (targeted "Example Program") program is running still make the counter - 1
  'set -1 value indicating program still running
End If
End Sub
0
 
Michael_DCommented:
Looks like AppActivate() Is sub and not function so it cannot return value.
0
 
BrianGEFF719Commented:
AppActivate title[, wait].

However, I dont know if you are familiar with Win32API, because what we can help you do is load the program yourself and change focus to it. APPACTIVATE is not a very efficent method of doing anything.


-Brian
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
xentixAuthor Commented:
AppActivate "Example Program", 1  wont work because then it just wants to have the program I'm building in focus to continue...

I do have a module added in with the available -
Declare Function SetFocusApi Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long

which I did see when I searched out Win32API, but I dont know hot to apply it...
SetFocus "Example Program"
--Wrong number of arguments or invalid property assignment
I definately dont know what to do with the function to make it focus...
0
 
trkcorpCommented:
I am not sure this will help but it will illustrate how to start, track and terminate an exe simply from w/in a VB app.

On a form place a button called cmdCalc.

Drop this code into the form:

Option Explicit
Dim rtn As Double

Private Sub cmdCalc_Click()
If rtn <> 0 Then
  On Error GoTo startnew
  AppActivate rtn
Else
startnew:
  Screen.MousePointer = vbHourglass
  rtn = Shell("calc.exe", vbNormalFocus)
  AppActivate rtn
  Screen.MousePointer = vbDefault
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo skipkill
AppActivate rtn
SendKeys "%{F4}", True   ' Send ALT+F4 to close Calculator.
skipkill:
Unload Me
End Sub
0
 
xentixAuthor Commented:
Using
  rtn = Shell("calc.exe", vbNormalFocus)
  AppActivate rtn
because its all I technically need,doesnt actually focus on the program, it loads multiple instances of the program...

And looking through the FaQ, I added in the coded to a module (which didnt work out - so I added it to the frm code itsself...)
Added into my code
GetForegroundWindow "Example Program"
And now get the error "only comments may appear after End Sub, End Function, and End Property" so it wont compile like this

A friend even suggested using
Declare Function FindWindow% Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)

But even the[ FindWindow "Example Program" ] doesnt work so hot...

Wow why does this seem so complicated? All I need to do is check to see if the "Calculator" is still running... Not itsself... Obviously if ITs not running, it cant check :-P
0
 
xentixAuthor Commented:
Ooooo that worked... Thanks for explaining that out, trkcorp. I'm fairly new to VB (like no one could notice) :)
0
All Courses

From novice to tech pro — start learning today.