VBScript将焦点放在IE中的窗口上 [英] VBScript set focus on a window in IE

查看:79
本文介绍了VBScript将焦点放在IE中的窗口上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在更新使用VBScript在IE中拉出一个窗口的旧代码.由于某些原因,它喜欢在IE后面打开.Google为我提供了以下几行代码,用于在VBScript中设置窗口焦点:

I'm updating an old piece of code that uses VBScript to pull up a window in IE. For some reason, it likes to open up behind IE. Google gave me the following couple lines for setting window focus in VBScript:

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

但是,当我在IE中运行此程序时,出现错误所需对象:'WScript'."

However, when I run this in IE, I get the error "Object required: 'WScript'."

在IE中是否有解决此问题的方法,或执行此操作的另一种方法?我已经打开并处理Word文档,没有任何问题.

Is there any way around this in IE, or another way to do this? I'm already opening and manipulating a Word document without any problem.

为澄清起见,我正在< script type ="text/vbscript">标签在浏览器(IE)中,并且代码甚至在我调用AppActivate之前的第一行就崩溃了.

To clarify, I am running this in a <script type="text/vbscript"> tag in the browser (IE), and the code is crashing on the first line, before I even call AppActivate.

更新:我的安全设置很低;所有ActiveX设置均处于启用状态(这是Intranet服务).我测试了这个问题中的代码,计算器打开没有问题.实际上,我使AppActivate可以与JavaScript一起使用,但不适用于VBScript.

Update: My security settings are pretty low; all ActiveX settings are on Enable (this is an intranet service). I tested the code from this question, and the calculator opened without issue. In fact, I got AppActivate to work with JavaScript, but it's not working with VBScript.

有效的JavaScript:

Working JavaScript:

<script type="text/javascript">
    function calcToFrontJ(){
        wshShell = new ActiveXObject("WScript.Shell");
        wshShell.AppActivate("Calculator");
    }
</script>

无法正常工作的VBScript:

Not Working 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发生了什么.

I guess I can always refactor to JavaScript, but I'd really like to know what's going on with this 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>

推荐答案

除非您使用以下方法自行创建,否则IE中将不存在WScript对象:
设置WScript = CreateObject("WScript.Shell")
但是,如果安全性设置不是很低,那就行不通了.

The WScript object does not exist in IE unless you create it yourself with:
Set WScript = CreateObject("WScript.Shell")
But it won't work if security settings are not at a quite low level.

考虑到Tmdean的评论,这是工作代码:

Factoring in Tmdean's comment, this is the working code:

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

这篇关于VBScript将焦点放在IE中的窗口上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆