total vbs scripting newb here. I\'m trying to automate closing a certain open window, namely a program called HostsMan. This is on Windows 8.1 Pro 64 bit, and this is what m
I've tried it, too, and couldn't get it to work. There must be something about the window class, perhaps, where AppActivate
doesn't see it as a top-level window?
In any event, AppActivate
also lets you pass the process ID instead of the window title. When I installed HostsMan, the process name was hm.exe
, so I'll use that in my example below.
Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")
For Each Process In Processes
If StrComp(Process.Name, "hm.exe", vbTextCompare) = 0 Then
' Activate the window using its process ID...
With CreateObject("WScript.Shell")
.AppActivate Process.ProcessId
.SendKeys "%{F4}"
End With
' We found our process. No more iteration required...
Exit For
End If
Next