使用通配符检查窗口是否打开的vbscript

时间:2016-09-12 17:10:26

标签: vbscript

我想检查一个窗口是否在vbscript中使用通配符打开。我能够找到下面的代码:

Set oShell = CreateObject("WScript.Shell") 
If oShell.AppActivate("Untitled - Notepad") Then
   WScript.Sleep 500
End If

但我想在窗口标题上使用通配符。我尝试使用*和%,但它不起作用。任何帮助表示赞赏。

If oShell.AppActivate("*Notepad*") Then

更新人员..我能找到一个解决方案,但如果有人可以简化这个问题,它仍然是开放的。谢谢。

Set Word = CreateObject("Word.Application")
Set Tasks = Word.Tasks

isFound = False
For i = 1 to 5
    For Each Task in Tasks
      checkVal = 0
      If Task.Visible Then
        checkVal = inStr(UCase(Task.name), UCase("outlook"))
        If checkVal <> 0 Then
            isFound = True
            Exit For
        End If
      End If
    Next
    If isFound = True Then
        Exit For
    End If
    WScript.Sleep 1000
Next

Word.Quit
msgbox ("Is the Window Found? - " & isFound)

1 个答案:

答案 0 :(得分:0)

查看此方法与创建单词doc。该方法需要依赖于安装办公室。这仅使用本机Windows库。

第一个功能:findwindowtitle 执行Tasklist命令以枚举和过滤标题列表。然后触发正则表达式解析器以使您的字符串与任务列表中的剩余值匹配。

第二个函数:matchtitle 然后继续使用将您的通配符转换为[a-zA-Z0-9.-]的正则表达式,这是包含空格的字母数字。通配符所需的种类以及允许的Windows文件字符。

MysearchString = "*Notepad"
processtitle = findwindowtitle(MysearchString)
wscript.echo "My search found window title: '" & processtitle & "'"
'do something with processtitle 


function findwindowtitle(srchstr)
    filtersrchstr = replace(srchstr, "*", "")
    strcommand = "tasklist /v | find /i """ & filtersrchstr & """"
    cmdout = CreateObject("Wscript.Shell").Exec("cmd /c """ & strcommand & " 2>&1 """).stdout.readall
    wscript.sleep 500
    findwindowtitle = matchtitle(srchstr, cmdout)
End Function

Function matchtitle(srchstr, input)
    matchtitle = false
    if instr(1, srchstr, "*", 1) <> 0 Then 
        filtersrchstr = replace(srchstr, "*", "")
        filterstrpatt = replace(srchstr, "*", "[a-zA-Z0-9\.- ]*")
    end if
    Set regex = CreateObject("VBScript.RegExp")
    regex.MultiLine = True
    regex.Global = True
    regex.IgnoreCase = True
    regex.Pattern = "(?:.*)(?:\d\d?\d?:\d\d:\d\d\s)(\b" & filterstrpatt & "\b)"
    Set matches = regex.Execute(input)
    for m = 0 to matches.count - 1
        Set SubMatches = matches.item(m).SubMatches
        for i = 0 to (Submatches.count - 1)
            if instr(1, Submatches.item(i), filtersrchstr, 1) <> 0 then matchtitle = Submatches.item(i)
        Next
    Next
    if (matchtitle = false) then
        wscript.echo "Could not find process with title matching, '" & srchstr & "'"
        wscript.quit
    end if
End Function