为什么我无法重新打开浏览器(对象)?

时间:2015-03-02 11:16:28

标签: internet-explorer vbscript process wmi

我想创建一个打开Internet Explorer浏览器的脚本,但有一些限制。脚本验证iexplorer.exe进程是否正在运行(表示浏览器已关闭),它会在10秒后自动重新打开。

这是剧本:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
Set objExplorer = CreateObject("InternetExplorer.Application")
Const REOPEN_AFTER =10000


objExplorer.Navigate "http://www.google.com"
objExplorer.Visible = true
objExplorer.ToolBar = false
objExplorer.MenuBar = false
objExplorer.StatusBar = false
objExplorer.AddressBar = true
objExplorer.Width = 1280
objExplorer.Height = 1024
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Resizable = false

Do While True
    Set colProcesses = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = 'iexplore.exe'")
    If colProcesses.Count = False Then
        objExplorer.Navigate "http://www.google.com" 
        objExplorer.Visible = true

    End If
    Wscript.Sleep REOPEN_AFTER
Loop

如果我启动它运行的脚本,则打开浏览器,但如果我关闭它,则不会重新打开它。

但是,如果我像这样运行它,那么它可以工作:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
Set objExplorer = CreateObject("InternetExplorer.Application")
Const REOPEN_AFTER =10000


objExplorer.Navigate "http://www.google.com"
objExplorer.Visible = true
objExplorer.ToolBar = false
objExplorer.MenuBar = false
objExplorer.StatusBar = false
objExplorer.AddressBar = true
objExplorer.Width = 1280
objExplorer.Height = 1024
objExplorer.Left = 0
objExplorer.Top = 0
objExplorer.Resizable = false

Do While True
    Set colProcesses = objWMIService.ExecQuery _
        ("Select * from Win32_Process Where Name = 'iexplore.exe'")
    If colProcesses.Count = 0 Then
        objShell.Run "iexplorer.exe"
    End If
    Wscript.Sleep REOPEN_AFTER
Loop

有人可以看到错误的位置吗?

2 个答案:

答案 0 :(得分:0)

if colProcesses.Count=0 Then

中移动对象实例化

当导航器关闭时,存储在变量中的对象引用无效。你需要一个新的实例。

答案 1 :(得分:0)

我认为当您关闭浏览器时,objExplorer无效并且您尝试

If colProcesses.Count = False Then
    objExplorer.Navigate "http://www.google.com" 
    objExplorer.Visible = true
End If

它会失败,因为它不再指向一个打开的浏览器 - 我认为你只需要在此时创建一​​个新的IE实例,例如:

function createExplorer

    Set createExplorer = CreateObject("InternetExplorer.Application")
    createExplorer.Navigate "http://www.google.com"
    createExplorer.Visible = true
    createExplorer.ToolBar = false
    createExplorer.MenuBar = false
    createExplorer.StatusBar = false
    createExplorer.AddressBar = true
    createExplorer.Width = 1280
    createExplorer.Height = 1024
    createExplorer.Left = 0
    createExplorer.Top = 0
    createExplorer.Resizable = false

end function



Do While True
    Set colProcesses = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'iexplore.exe'")
    If colProcesses.Count = 0 Then
        Set objExplorer = createExplorer()
    End If
    Wscript.Sleep REOPEN_AFTER
Loop