2011-05-13 49 views
5

我正在更新一段使用VBScript在IE中拉起窗口的代碼。出於某種原因,它喜歡在IE後面開放。谷歌給了我在VBScript設置窗口焦點下面幾行:VBScript將焦點放在IE中的窗口上

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

然而,當我在IE中運行它,我得到的錯誤「所需的對象:‘WScript的’。」

在IE中有沒有解決這個問題的方法,或者其他方法來做到這一點?我已經打開並操作Word文檔沒有任何問題。

編輯:爲了澄清,我在<腳本類型運行此=「文/ VBSCRIPT」 >標籤在瀏覽器(IE),代碼崩潰在第一行,之前我甚至打電話使用AppActivate。

更新:我的安全設置很低;所有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> 
+0

AppActivate使用窗口標題欄中的文本進行搜索。你使用的是什麼代碼? – Tmdean 2011-05-13 15:40:49

+0

@Tmdean我的意思是代碼在'

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 

我看了acouple小時今天淨並張羅這個代碼。它實際上工作:D。

+0

雖然這個答案可能是正確和有用的,但是如果你包含一些解釋並解釋它如何幫助解決問題,那麼它是首選。如果存在導致其停止工作並且用戶需要了解其曾經工作的變化(可能不相關),這在未來變得特別有用。 – 2015-03-05 23:18:18

+0

我個人會接受這個答案。這是我能找到的最可靠的答案。 +1 :) – 2018-01-07 06:01:43