How can I close a window wi开发者_如何学Pythonth a specific title in Windows XP base using VBscript?
Or is there another way to solve this problem?
You can use the SendKeys
method to send the Alt+F4 shortcut to the window you wish to close. This window must be active at the moment, so you also need to call AppActivate
right before SendKeys
.
Basically, you'll need something like this:
Set oShell = CreateObject("WScript.Shell")
oShell.AppActivate "Untitled - Notepad"
oShell.SendKeys "%{F4}"
You may want to add checks and small delays to make your script more foolproof:
Set oShell = CreateObject("WScript.Shell")
If oShell.AppActivate("Untitled - Notepad") Then
WScript.Sleep 500
oShell.SendKeys "%{F4}"
End If
Edit: (An answer to your comment/question about VBScript resources.)
I've compiled some links to VBScript websites and resource pages that I hope they will be helpful:
Learning
- Scripting: Your First Steps
- Sesame Script (CHM download)
- VBScript Tutorial on W3Schools (note: this tutorial is oriented on browser scripting, not desktop scripting)
References
- VBScript User's Guide & Reference in MSDN (CHM donwload)
Other resources
- TechNet Script Center (CHM download)
- TechNet Script Repository (CHM download)
- Hey, Scripting Guy! blog (CHM arvhive)
- Rob van der Woude's Scripting Pages
- Also, check out VBScript questions here on Stack Overflow
As for VBScript resources in Russian, check out script-coding.info and Серый форум — there're lots of useful and interesting examples. Also, take a look at the this thread, which contains links to many VBScript resources, including those in Russian.
Posting this answer for anyone who is still stuck on trying to close the WScript.Shell Object after creating it and not able to find a solution. I tried the above solution and it cause MSWord 2016 to crash, don't know the reason My Vb Script :
Dim wsh As Object
Set wsh = CreateObject("WScript.Shell", vbNothing)
wsh.Run "cmd.exe /C pause"
wsh.Run "taskkill /F /IM cmd.exe"
精彩评论