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

后端 未结 5 1003
谎友^
谎友^ 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
    
    0 讨论(0)
  • 2020-12-19 01:09

    The key here is to put a small pause after the 'run' command but before the 'appactivate' command so the application shows up in the process list.

         WshShell.Run "calc"
         WScript.Sleep 100
         WshShell.AppActivate "Calculator"
    
    0 讨论(0)
  • 2020-12-19 01:15

    To solve the problem of AppActivate you have to use loop and if condition statement to check if the active windows is the target windows or not because sometime you deselect or the system deselect the target windows before execute the sendkeys directly so you got error.

    I create this tight strict way

    Set WshShell = CreateObject("WScript.Shell")
        for i=0 to 300 ' this loop will continue about 30 sec if this not enough increase this number
            Rtn=WshShell.AppActivate("HostsMan") ' HostMan have to be the windows title of application or its process ID
            If Rtn = True Then 
                WshShell.SendKeys "%{F4}"    ' send key to click ALT+F4 to close 
                wscript.sleep 100        ' stop execute next line until finish close app
                Rtn=WshShell.AppActivate("HostsMan") 
                 If Rtn=False Then ' using nested If to sure of selected windows is false because its close 
                   Exit For      ' exit for loop directly and execute what after for next
                 End If 
            End If
            wscript.sleep 100
        Next
    
    0 讨论(0)
  • 2020-12-19 01:29

    Alternative solution using WMIService (no loop through all processes required):

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * from Win32_Process WHERE Name = '" & ProcessName & "'") 
    
    If colItems.Count = 0 Then
        WScript.Echo "Process not found"
        Wscript.Quit
    End If
    
    For Each objProcess in colItems
        WshShell.AppActivate(objProcess.ProcessId)
        Exit For
    Next
    
    0 讨论(0)
  • 2020-12-19 01:31
    Dim sh : Set sh =CreateObject("Wscript.Shell")
    sh.Exec "calc.exe"
    Wscript.Sleep 1000
    sh.Exec "taskkill /f /FI ""WINDOWTITLE  eq ""calc*"""
    

    OR

    sh.Run "taskkill /f /im calc.exe",0,False
    
    0 讨论(0)
提交回复
热议问题