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!

Close an application, only if it's open?

Status
Not open for further replies.

cold25

Programmer
Feb 11, 2002
41
US
Hi. My vb6 program launches a program (Acrobat Reader, for example), and then does some stuff to the opened program. When the vb6 program ends, it automacially closes the program that it launched earlier. However, the problem is that if the user closes the program that is launched by the vb6 program, and then tries to end the vb6 program, the vb6 program crashes, because it is trying to close a program that is no longer open.

So in attempting to solve this, I am trying to put in a simple "If" statement that would make the vb6 program only try to close the program that it launched, if that program is still open. I read that the AppActivate command returns a boolean value. So, I was thinking that something like the following might work:

If AppActivate "Acrobat Reader" = True Then
Call fSendKeys("%{F4}")
End If

However, it keeps calling the compile error: "Expected: Then or GoTo". I've also tried changing it to:
If AppActivate ("Acrobat Reader") = True Then
but it says "Expected Function or variable". I just want a statement that says "If the program's open, close it. If it's not, don't." Any ideas as to what I might be doing wrong here? Thanks in advance.
cold25
 
This may be a little lame, but.... You could add error handling with a resume next. Then, when your program tries to close an application that is already closed, it'll error, but get caught by the error handler.

It's not elegant, but it should work.

Ex.


On error Goto MyErrorHandler

' Code to close the running application

Exit Sub

MyErrorHandler:
Resume Next
 
You could also look at thread222-63270

or if your systems are XP you could look into using WMI.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top