6

I was trying to open and close an application. I tried like this:

Dim App1 
Set App1 = CreateObject("WScript.Shell")
App1.Run("firefox")
App1.Quit

Firefox will open, but it will not close.

Error message:

object doesn't support this property or method

I referred InDesign Scripting how to quit application (not document)

Please tell me the procedure to close the application.

Community
  • 1
  • 1
user1915934
  • 121
  • 2
  • 2
  • 7
  • Can't we close a previously opened application (also opened by VBScript)? (application running in foreground) – user152435 Apr 06 '17 at 19:32
  • Quitting a vbscript application would simply be a `Wscript.Quit` - so that part doesn't really apply. Firefox is not a vbscript application. – SDsolar Nov 15 '17 at 07:46

5 Answers5

7

If you want to be able to terminate a process that way you need to use the Exec method instead of the Run method.

Set ff = CreateObject("WScript.Shell").Exec("firefox")
'you do stuff
ff.Terminate
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
3

I observed a few ways:

  1. taskkill - Worked only if I try to kill the same process, as I run.

    Dim oShell : Set oShell = CreateObject("WScript.Shell")
    oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"
    
    'some code
    
    oShell.Run "taskkill /f /im ddl.exe", , True
    
  2. SendKeys - Works for all app, if the window name is known.

    Dim oShell : Set oShell = CreateObject("WScript.Shell")
    filename = "C:\Some_file.txt - Notepad"
    act = oShell.AppActivate(fileName)
    oShell.SendKeys "% C"
    
  3. WMI object - 2 ways above worked good for me so I didn't try it. You can find an example here:

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\.\root\cimv2")
    
    Set colProcessList = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = 'Chrome.exe'")
    
    Set oShell = CreateObject("WScript.Shell")
    For Each objProcess in colProcessList
        oShell.Run "taskkill /im chrome.exe", , True
    Next
    
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
vr286
  • 826
  • 7
  • 6
2

Here is an alternative VBScript implementation:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")

For Each objProcess in colProcessList
    objProcess.Terminate()
Next
Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
1

Firefox is not a COM object. There is no Firefox COM object. Firefox does not want you to use COM. Or NET. Or Microsoft. That is why you could not create a Firefox object, so you created a WScript.Shell object instead.

WScript.Shell does not have a quit method. If it did, it wouldn't help kill Firefox.

This is an example of using WMI from VBS to start, then kill a process like Firefox.

david
  • 2,435
  • 1
  • 21
  • 33
0

Good work for this example:

Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\My_Scripts\ddl.exe"" -p1 -c"

'some code

oShell.Run "taskkill /f /im ddl.exe", , True
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Farouk
  • 1
  • 1
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Sep 05 '17 at 13:49