VBScript如何在IE上将焦点设置到窗口?

5

我正在更新一段使用VBScript在IE中打开窗口的旧代码。不知何故,它喜欢在IE后面打开。谷歌给了我以下几行VBScript设置窗口焦点的代码:

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("calculator")

然而,在IE浏览器中运行时,我收到了“对象必需:'WScript'”的错误消息。是否有任何绕过此错误的方法或其他方法可以实现这一功能?我已经成功打开和操作了一个Word文档。
编辑:澄清一下,我在浏览器(IE)中运行了这个代码段,并且代码在第一行崩溃,甚至在我调用AppActivate之前就崩溃了。 更新:我的安全设置很低;所有ActiveX设置都是启用的(这是一个内部服务)。我测试了这个问题中的代码,计算器没有问题地打开了。事实上,我已经用JavaScript使AppActivate工作了,但它不能用VBScript。以下是可行的JavaScript代码:
<script type="text/javascript">
    function calcToFrontJ(){
        wshShell = new ActiveXObject("WScript.Shell");
        wshShell.AppActivate("Calculator");
    }
</script>

VBScript无法工作:

<script type="text/vbscript">
    Public Function calcToFrontV()
        'Set WScript = CreateObject("WScript.Shell") 'breaks with or without this line
        Set WshShell = WScript.CreateObject("WScript.Shell")
        WshShell.AppActivate("Calculator")
    End Function
</script>

我想我总可以重构到JavaScript,但我真的很想知道这个VBScript发生了什么。

最终答案:

<script type="text/vbscript">
    Public Function calcToFrontV()
        'must not use WScript when running within IE 
        Set WshShell = CreateObject("WScript.Shell")
        WshShell.AppActivate("Calculator")
    End Function
</script>

AppActivate 使用窗口标题栏中的文本进行搜索。你在使用什么代码来操作 IE? - Tmdean
@Tmdean 我的意思是代码在<script type="text/vbscript">标签中,并在浏览器中运行。我实际上是使用“WINWORD”(进程名称)调用AppActivate,但是当我尝试创建WshShell对象的第一行代码崩溃了。 - jtpereyda
只有在使用Windows脚本宿主(使用wscript.exe或cscript.exe)运行脚本时才可以使用WScript。此外,由于该对象未标记为安全脚本,因此存在安全限制,防止您在网页中获取WScript.Shell对象的访问权限。如果网页上运行的脚本可以使用它,那将非常危险。请参见https://dev59.com/i0nSa4cB1Zd3GeqPLSjv#1363155。 - Tmdean
@Tmdean 感谢提供的信息。我正在为一个内部网络编程,看起来默认设置相当宽松。这个问题表明我应该启用所有ActiveX内容,而且所有这些设置都已经被设置为启用。 - jtpereyda
3个回答

2

在IE浏览器中,如果没有手动创建WScript对象,则该对象不存在。创建方法如下:
Set WScript = CreateObject("WScript.Shell")
但是,如果安全设置不是相当低的水平,它将无法正常工作。

编辑:根据Tmdean的评论,以下是可行的代码:

'CreateObject("WScript.Shell")
Set wshShell = CreateObject("WScript.Shell")
wshShell.AppActivate("calculator")

我也发现了这个问题,但它给了我另一个错误信息:“对象不支持此属性或方法:'WScript.CreateObject'”。 - jtpereyda
你知道具体的安全设置是什么吗?我的设置相当低(在安全设置下几乎所有功能都已启用)。请参见问题的评论。 - jtpereyda
1
我认为你仍在使用WScript.CreateObject("WScript.Shell")。你应该只使用CreateObject("WScript.Shell")。 - Tmdean

2
Set objShell = WScript.CreateObject("WScript.Shell")
Set objIE = WScript.CreateObject("InternetExplorer.Application", "IE_")
objie.navigate "url"
objIE.Visible = 1
objShell.AppActivate objIE

'Above opens an ie object and navigates
'below runs through your proccesses and brings Internet Explorer to the top.

Set Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")

intProcessId = ""
For Each Process In Processes
    If StrComp(Process.Name, "iexplore.exe", vbTextCompare) = 0 Then
        intProcessId = Process.ProcessId
        Exit For
    End If
Next

If Len(intProcessId) > 0 Then
    With CreateObject("WScript.Shell")
        .AppActivate intProcessId

    End With
End If

我今天在网上搜索了几个小时,收集了这段代码。它实际上是起作用的 :D。

虽然这个答案可能是正确和有用的,但最好还是附上一些解释来说明它如何帮助解决问题。如果将来发生了变化(可能与此无关),导致它停止工作并且用户需要了解它曾经是如何工作的,这将特别有用。 - Kevin Brown-Silva
我个人会接受这个答案。这是我能找到的最可靠的答案。+1 :) - user4821390

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接