WshShell.AppActivate doesn't seem to work in simple vbs script

后端 未结 5 1016
谎友^
谎友^ 2020-12-19 00:58

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

5条回答
  •  抹茶落季
    2020-12-19 01:09

    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
    

提交回复
热议问题