Q:

VBScript Closing Application Not Working

I am currently learning VBScript and I have written this script just to practise and I am having trouble closing more then one application (notepad and calc) within one script. When I run the applications by itself in a seperate script they close. But when I combined everything together in this script they don't close. Am I doing something wrong with WMI Service?
'*** calc.vbs ***

Set WshShell = CreateObject("WScript.Shell")

WshShell.Run "calc"

WScript.Sleep 1500

WshShell.AppActivate "Calculator"

WScript.Sleep 1500

WshShell.SendKeys "5"

WScript.Sleep 1000

WshShell.SendKeys "{+}"

WScript.Sleep 1000

WshShell.SendKeys "10{+}"

WScript.Sleep 1000

WshShell.SendKeys "12"

WScript.Sleep 1000

WshShell.SendKeys "{=}"

WScript.Sleep 5000

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process 

Where Name = 'calc.exe'")

For Each objProcess In colProcessList

    objProcess.Terminate

Next

'*** notepad.vbs ***

WScript.Sleep 5000

WshShell.Run "notepad"

WshShell.AppActivate "Untitled - Notepad"

WScript.Sleep 1000

WshShell.SendKeys "This is the first line "

WScript.Sleep 1000

WshShell.SendKeys "~" ' same as {Enter}

WScript.Sleep 1000

WshShell.SendKeys "This is the second line"

WScript.Sleep 1000

WshShell.SendKeys "{Enter}"

WScript.Sleep 1000

WshShell.SendKeys "This is the third line"

WScript.Sleep 5000

Set colProcessListt = objWMIService.ExecQuery("SELECT * FROM Win32_Process 

Where Name = 'notepad.exe'")

For Each objProcesss In colProcessListt

    objProcesss.Terminate

Next

A:

Your problem is caused by a combination of a good (Option Explicit) and some very bad (global On Error Resume Next, dimming variables in long lists at the top/far from their actual use, and lunatic variable names (objProcess, objProcesss, ...)) things. To show that, I'll use a very simple technique to keep 'a lot of different interesting pieces of code in one file'.
Option Explicit

' global consts and variables (not needed here)

' poor man's dispatch; use re-order or comments to choose current piece of work
WScript.Quit Literals()
WScript.Quit CloseCalcBad()
WScript.Quit CloseCalc()
WScript.Quit BadVars()

' can do literals; even date literals; have thought about Locale
Function Literals()
  Literals = 1
  Dim literalNumeric : literalNumeric = 20
  Dim literalString : literalString = "BeachBall"
  Dim literalDate : literalDate = #1/2/3#
  WScript.Echo "literalNumeric 20:", literalNumeric
  literalNumeric = 12.34
  WScript.Echo "literalNumeric 12.34:", literalNumeric, GetLocale()
  WScript.Echo "literalString: ""BeachBall""", literalString
  WScript.Echo "literalDate #1/2/3#:", literalDate, GetLocale()
  Literals = 0
End Function

' can't work with undefined vars like strComputer etc.
Function CloseCalcBad()
  CloseCalcBad = 1
  WScript.Echo "CloseCalcBad()"
  Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
  WScript.Echo "start calc"
  WshShell.Run "calc"
  WScript.Echo "waiting"
  WScript.Sleep 5000

  WScript.Echo "trying to close"
  On Error Resume Next
' if you comment out the (evil) OERN, you'll get
' Microsoft VBScript runtime error: Variable is undefined: 'strComputer'
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = 'calc.exe'")
  For Each objProcess In colProcessList
      WScript.Echo "in loop"
      WScript.Echo TypeName(objProcess)
      objProcess.Terminate
  Next
  WScript.Echo "done"
  CloseCalcBad = 0
End Function

' no OERN, no undefined vars, IT WORKS!
Function CloseCalc()
  CloseCalc = 1
  WScript.Echo "CloseCalc()"
  Dim WshShell : Set WshShell = CreateObject("WScript.Shell")
  WScript.Echo "start calc"
  WshShell.Run "calc"
  WScript.Echo "waiting"
  WScript.Sleep 5000

  WScript.Echo "trying to close"
  Dim strComputer : strComputer = "."
  Dim objWMIService : Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Dim colProcessList : Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process Where Name = 'calc.exe'")
  Dim objProcess
  For Each objProcess In colProcessList
      WScript.Echo "in loop"
      WScript.Echo TypeName(objProcess)
      objProcess.Terminate
  Next
  WScript.Echo "done"
  CloseCalc = 0
End Function

' it's difficult to keep track of bad vars (undefined vars hidden by OERN)
Function BadVars()
  BadVars = 1
  Dim s1 : s1 = "Prefix"
 On Error Resume Next
  s2 = "Suffix"
  WScript.Echo "s1:", s1
  WScript.Echo "s2:", s2 ' statement/line skipped, because s2 can't be accessed
  WScript.Echo "where did s2 output go?"
  BadVars = 0
End Function

Such a script deals with different topics/problems in (short) functions that return 1 (failure) or 0 (success) to the top level code (and from there to the OS). By re-ordering or commenting you can pick the special function/topic/problem you are currently working on. If a question leads to another, you can add a function, develop a solution, test it, and return to the function you startet with. 
Because the functions can be short and to the point, you are encouraged to use local variables and dim them just before use; no need funny names.
The Literals() function began with your code; I added date literals to demonstrate how you can refactor/improve/enhance functions if you learn something new.
CloseCalcBad() - and its output:
cscript 41169790.vbs
CloseCalcBad()
start calc
waiting
trying to close
in loop
done

shows that the OERN hides the "var not defined" errors, skips statements/lines where bad vars are used (WScript.Echo TypeName(objProcess)), but proudly presents diagnostic output like "done".
CloseCalc() Dims the variables used and therefore closes Calc - output:
cscript 41169790.vbs
CloseCalc()
start calc
waiting
trying to close
in loop
SWbemObjectEx   <-- have something to terminate!
done

I include BadVars() - 
cscript 41169790.vbs
s1: Prefix
where did s2 output go?

to show the evil global OERN without the additional complexity of WMI.

