neville99
-
Total Posts
:
1
- Scores: 0
-
Reward points
:
0
- Joined: 12/13/2011
-
Status: offline
|
VBscript to check for running applications and prompt user to close
Tuesday, December 13, 2011 3:44 PM
( permalink)
Hi all, I need to create a script to check if Outlook, word, and excel are running, and if so prompt the user to close them. I also don't want the user to be able to cancel out of this, and the ok button needs to have intelligence if a user clicks ok w/o closing their apps or only closes 1 of them, they are prompted to close the remaining apps. After that i would add a wshshell.run line to install an app. I know how to do this for 1 app and prompt the user, but not sure how to do this for 3 apps, and also build an intelligent "ok" button. Any help would be appreciated. Thanks
|
|
|
|
59cobalt
-
Total Posts
:
981
- Scores: 91
-
Reward points
:
0
- Joined: 7/17/2011
-
Status: offline
|
Re:VBscript to check for running applications and prompt user to close
Wednesday, December 14, 2011 8:04 AM
( permalink)
Post the code you have. Checking if an Office application is running can be done with something like this: Function IsRunning(name)
On Error Resume Next
Dim app : Set app = GetObject(, name & ".Application")
Select Case Err.Number
Case 0
IsRunning = True
Set app = Nothing
Case 429
IsRunning = False
Case Else
WScript.Echo "Unexpected error: " & Err.Description & " (" & Err.Number & ")"
WScript.Quit 1
End Select
On Error Goto 0
End Function
If IsRunning("Excel") Then WScript.Echo "Excel is running."
|
|
|
|
Hackoo
-
Total Posts
:
105
- Scores: 4
-
Reward points
:
0
- Joined: 6/25/2010
-
Status: offline
|
Re:VBscript to check for running applications and prompt user to close
Wednesday, December 14, 2011 7:12 PM
( permalink)
You should write something like this 'FindApp+Kill_All.vbs Const ForAppending= 'Check if Wscript is running, we exit, otherwise we run the script 'Vérifie si Wscript est en cours d'exécution, on quitte, sinon on lance le script If CheckIfWscriptIsRunning Then Wscript.Quit WaitAndLoopForApp(10) '10 is the Number of seconds to wait to find an ' application to be Killed. We can increase this time. ' 10 est le Nombre de secondes d'attente pour rechercher une ' application à terminer. On peut augmenter cette durée. Sub FindApp(strApp) Set fso = Wscript.CreateObject("Scripting.FilesystemObject") set fich = fso.OpenTextFile("C:\Events.log",ForAppending,true) ' Fichier journal Set SRVC = GetObject("winmgmts:\\") Set ObjServ = SRVC.InstancesOf("Win32_Process") For Each myObj In ObjServ If LCase(myObj.Name) = LCase(strApp) Then 'Writing in the custom log file 'Ecriture dans le fichier journal personnalisé Fich.Write myObj.Name +" is running but killed at : " & Now & vbnewline ' We close the application and we get out of the loop ' On ferme l'application et on sort de la boucle IF Not (myObj Is Nothing) Then myObj.Terminate() Exit For End If Next Set ObjServ=Nothing Set SRVC = Nothing End Sub Sub WaitAndLoopForApp(ByVal Sec) start=timer do i=start+timer loop until i-start>=Sec 'We wait a number of secodes to act ' On attend un nombre (sec) de secondes pour agir If i-Start>=Sec then Do FindApp("Winword.exe") FindApp("proccess_name_of_outlook.exe") FindApp("process_name_of_Excel.exe") 'and so on Wscript.Sleep 10000 Set WS=CreateObject("Wscript.Shell") Com = "%SystemRoot%\System32\CScript.exe " & Wscript.ScriptFullName Result=WS.Run(Com,0,True) Loop End If End Sub Function CheckIfWscriptIsRunning() ' Vérifie si Wscript tourne ou non Set SRVC = GetObject("winmgmts:\\") Set ObjServ = SRVC.InstancesOf("Win32_Process") s="" For Each myObj In ObjServ If LCase(myObj.Name) = "wscript.exe" then s= s+ myObj.name + VbCrLf End If OK=InsTr(1,s,VbCrLf+"wscript.exe",1) '>=1 Next CheckIfWscriptIsRunning=OK End Function
|
|
|
|