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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shut down all instances of Excel 1

Status
Not open for further replies.

kyletreyfield

Technical User
Jun 12, 2008
27
US
I've been doing a lot of VBA scripting for a project I'm working on and inevitably the program crashes and I have an instance of Excel open. I can go into the task manager and close it, but when everything gets automated, I won't be able to do this.

Is there any way I can go through and shut down all running instances of Excel through VFP?

Thanks
 
Try this (Note: You do not have to supply the path to the EXE, just the name):

Code:
*!*    Check if EXE is running and optionally terminate it
FUNCTION IsExeRunning(tcExeName AS String, tlTerminate AS Logical) AS Logical
   LOCAL loLocator, loWMI, loProcesses, loProcess, llIsRunning

   loLocator = CREATEOBJECT("WBEMScripting.SWBEMLocator")
   loWMI = loLocator.ConnectServer() 
   loWMI.Security_.ImpersonationLevel = 3
 
   loProcesses = loWMI.ExecQuery([SELECT * FROM Win32_Process WHERE Name = '] + tcExeName + ['])

   IF loProcesses.Count > 0
      llIsRunning = .T.

      FOR EACH loProcess IN loProcesses
          IF tlTerminate
             loProcess.Terminate(0)
          ENDIF
      ENDFOR
  ENDIF

  RETURN llIsRunning

ENDFUNC

You can expand on this to pass a reference parm to determine that the EXE *was* running and *was* terminated.
 
Vernspace's code looks like it will do exactly what you want. Just keep in mind that it will also close any non-automated instances of Excel, in other words, if the user launched Excel interactively rather than through your app. (At least, I think that's right. Vernspace, please correct me if I am wrong.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top