2015-03-02 64 views
0

我想創建一個腳本來打開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 

有人能看到的錯誤是什麼?

回答

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 
0

移動對象實例化的if colProcesses.Count=0 Then

內。當導航器被關閉時,存儲在該變量中的對象的引用是無效的。你需要一個新的實例。

相關問題