【问题标题】:VBScript set focus on a window in IEVBScript 将焦点设置在 IE 中的窗口上
【发布时间】:2011-08-24 13:36:33
【问题描述】:

我正在更新一段旧代码,它使用 VBScript 在 IE 中打开一个窗口。出于某种原因,它喜欢在 IE 后面打开。 Google 给了我以下几行用于在 VBScript 中设置窗口焦点:

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

但是,当我在 IE 中运行它时,我收到错误“需要对象:'WScript'。”

在 IE 中有什么方法可以解决这个问题,或者有其他方法吗?我已经可以毫无问题地打开和操作 Word 文档了。

编辑:澄清一下,我在浏览器 (IE) 的

更新:我的安全设置很低;所有 ActiveX 设置都启用(这是一个 Intranet 服务)。我测试了this问题的代码,计算器打开没有问题。事实上,我让 AppActivate 可以与 JavaScript 一起使用,但它不能与 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 我的意思是代码在&lt;script type="text/vbscript"&gt; 标记中并在浏览器中运行。我实际上是用“WINWORD”(进程名称)调用 AppActivate,但代码在我尝试创建 WshShell 对象的第一行崩溃。
  • WScript 仅在您使用 Windows 脚本主机(使用 wscript.exe 或 cscript.exe)运行脚本时可用。此外,还有一些安全限制会阻止您访问网页中的 WScript.Shell 对象,因为该对象未标记为可安全编写脚本。如果在网页上运行的脚本可以使用它,那将是非常危险的。见stackoverflow.com/questions/1363095/…
  • @Tmdean 感谢您提供的信息。我正在为 Intranet 编程,看起来默认设置相当宽松。 This question 说我应该启用所有 ActiveX 的东西,并且所有这些设置都已设置为启用。

标签: internet-explorer vbscript setfocus wsh


【解决方案1】:
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。

【讨论】:

  • 虽然这个答案可能是正确且有用的,但最好在其中附上一些解释来解释它如何帮助解决问题。如果有更改(可能不相关)导致它停止工作并且用户需要了解它曾经是如何工作的,这在未来变得特别有用。
  • 我个人会接受这个答案。这是我能找到的最可靠的答案。 +1 :)
【解决方案2】:

IE 中不存在 WScript 对象,除非您自己使用以下命令创建它:
Set WScript = CreateObject("WScript.Shell")
但是如果安全设置不是非常低的级别,它就不会起作用。

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

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

【讨论】:

  • 我也发现了,但它给了我另一个错误消息,“对象不支持此属性或方法:'WScript.CreateObject'”。
  • 您知道具体的安全设置吗?我的设置非常低(几乎所有内容都在安全设置下启用)。请参阅对问题的评论。
  • 我认为您仍在使用 WScript.CreateObject("WScript.Shell")。您应该只使用 CreateObject("WScript.Shell")
【解决方案3】:

诀窍是使用WScript.CreateObject() 而不是普通的CreateObject() 来创建IE 对象。

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

附:我从 https://groups.google.com/forum/#!msg/microsoft.public.scripting.vbscript/SKWhisXB4wY/U8cwS3lflXAJ 的 Dan Bernhardt 那里得到了解决方案

【讨论】:

    猜你喜欢
    • 2011-11-11
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多