Alexandr
Alexandr

Reputation: 117

Get title opening windows and close with specific title?

How can I close a window with a specific title in Windows XP base using VBscript?

Or is there another way to solve this problem?

Upvotes: 3

Views: 33529

Answers (2)

Ritesh  Karwa
Ritesh Karwa

Reputation: 2254

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"

Upvotes: 0

Helen
Helen

Reputation: 97580

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

References

Other resources


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.

Upvotes: 12

Related Questions