2014-02-15 29 views
0

目前我是一個學習HTA編程。我有一個要求如下。使用生成的URL啓動Internet Explorer的VBScript

表單中有兩個輸入框(均爲必填項)。當我輸入數值並單擊搜索按鈕時,將根據輸入創建一個URL。應該使用生成的URL啓動Internet Explorer應用程序。

我的問題是,我能夠啓動IE瀏覽器,但我無法將URL傳遞給它。 我嘗試了很多方法,但我無法完成。

我在下面給出了我的代碼。我已經刪除了將URL傳遞給瀏覽器的錯誤語法。下面的代碼會創建url並啓動一個空白的IE瀏覽器。

請幫幫我。提前致謝。

<html> 
<head> 
<script language="VBScript"> 

    Sub RunProgram 
     callb = document.getElementById("callb").value 
     call = document.getElementById("call").value 
     url = "www.google.com"&callb&"and"&call 
     msgbox(url) 
     Set objShell = CreateObject("Wscript.Shell") 
     objShell.Run "iexplore.exe" 
    End Sub 

</script> 
</head> 
<body> 

<form style="width:254px; height:44px;"> 
CallB: <input type="text" id="callb" value=""><br> 
Call : <input type="text" id="call" value=""><br><br> 
<button onclick="RunProgram">search</button> 
</form> 

</body> 
</html> 

回答

3

您可以通過使用WShell的Run()方法加載默認瀏覽器的URL:

CreateObject("WScript.Shell").Run "www.google.com" 

如果你想顯式地加載在Internet Explorer中的URL,你需要確定的完整路徑首先IEXPLORE.EXE,然後將它傳遞到運行():

CreateObject("WScript.Shell").Run """" & strExePath & """ www.google.com" 

或者

CreateObject("WScript.Shell").Run """" & strExePath & """ " & strURL 

請注意引號。如果路徑中包含空格,您需要在IE的路徑中加引號。爲了在字符串中指定一個報價,你必須加倍。如果報價是混亂的,您可以使用CHR(34)來指定一個引號字符:

CreateObject("WScript.Shell").Run Chr(34) & strExePath & Chr(34) & " " & strURL 
+0

你真的很棒。它爲我工作。我有一個小問題。你能否在語法上清楚地解釋一下引號? – arunpandiyarajhen

+4

當然。在命令行鍵入路徑時,如果路徑包含空格,則必須使用引號。例如,「C:\ Program Files」。同樣的規則適用於Run()函數。要在字符串中指定一個引號,它必須加倍。這就是爲什麼我們使用4個引號。一個開始字符串,一個雙引號,然後另一個引號結束字符串。或者,您可以使用Chr(34)來指定報價。我編輯了我的答案以顯示此方法。 – Bond

+0

我明白了。非常感謝。 :) – arunpandiyarajhen

0

爲了使生活更輕鬆,我通常使用此功能在一個變量加雙引號。

Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 

一個示例,向您展示如何使用它。

Option Explicit 
Dim fso,ws,DossierProgramfiles,Winrar,FileZilla,FireFox 
Set fso = CreateObject("Scripting.FileSystemObject") 
Set ws = CreateObject("WScript.Shell") 
DossierProgramfiles = ws.ExpandEnvironmentStrings("%PROGRAMFILES%") 
Winrar = DblQuote(DossierProgramfiles & "\Winrar\Winrar.exe") 
If fso.FileExists(DossierProgramfiles & "\Winrar\Winrar.exe") Then 
    MsgBox Winrar,Vbinformation,Winrar 
    ws.run Winrar 
Else 
    MsgBox "Le fichier " & Winrar & " n'existe pas",VbCritical,"Le fichier " & Winrar & " n'existe pas" 
End If 
FileZilla = DblQuote(DossierProgramfiles & "\FileZilla FTP Client\filezilla.exe") 
If fso.FileExists(DossierProgramfiles & "\FileZilla FTP Client\filezilla.exe") Then 
    MsgBox FileZilla,Vbinformation,FileZilla 
    Ws.run FileZilla 
Else 
    MsgBox "Le fichier " & FileZilla & " n'existe pas",VbCritical,"Le fichier " & FileZilla & " n'existe pas" 
End If 
FireFox = DblQuote(DossierProgramfiles & "\Mozilla Firefox\FireFox.exe") 
If fso.FileExists(DossierProgramfiles & "\Mozilla Firefox\FireFox.exe") Then 
    MsgBox FireFox,Vbinformation,FireFox 
    ws.run FireFox 
Else 
    MsgBox "Le fichier " & FireFox & " n'existe pas",VbCritical,"Le fichier " & FireFox & " n'existe pas" 
End If 
'**************************************************************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'**************************************************************************************************** 
0

給它這個HTA一試:

<html> 
<head> 
<HTA:APPLICATION 
APPLICATIONNAME="Search on Google with Internet Explorer" 
BORDER="THIN" 
BORDERSTYLE="NORMAL" 
ICON="magnify.exe" 
INNERBORDER="NO" 
MAXIMIZEBUTTON="NO" 
MINIMIZEBUTTON="NO" 
SCROLL="NO" 
SELECTION="NO" 
SYSMENU="YES" 
SINGLEINSTANCE="YES"/> 
<META HTTP-EQUIV="MSThemeCompatible" CONTENT="YES"> 
<script language="VBScript"> 
Option Explicit 
Dim Titre 
Titre = "Search on Google with Internet Explorer" 
Self.document.title = Titre 
Sub window_onload() 
    CALL CenterWindow(300,180) 
    Self.document.bgColor = "Orange" 
End Sub 
Sub RunProgram() 
    Dim X,Y,URL,objShell,Param,MaCmd 
    X = text1.value 
    Y = text2.value 
    Param = "#q="& X & "+" & Y &"" 
    URL = "www.google.com"& Param 
    Set objShell = CreateObject("Wscript.Shell") 
    msgbox("iexplore.exe " & DblQuote(URL)),VbInformation,Titre 
    MaCmd = "iexplore.exe "& DblQuote(URL) &"" 
    objShell.Run(MaCmd) 
End Sub 
'*************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'*************************************************** 
'Position Windows 
Sub CenterWindow(x,y) 
    Dim iLeft,itop 
    window.resizeTo x,y 
    iLeft = window.screen.availWidth/2 - x/2 
    itop = window.screen.availHeight/2 - y/2 
    window.moveTo ileft, itop 
End Sub 
</script> 
</head> 
<body><center> 
Text1 : <input type="text" id="text1" Name="text1" value="Hackoo"><br> 
Text2 : <input type="text" id="text2" Name="text2" value="Vbscript+HTA"><br><br> 
<input type="submit" Value="Search on Google" onclick="RunProgram()"></center> 
</body> 
</html> 
0

下面的代碼片段將在默認瀏覽器中啓動此網址。

CreateObject("WScript.Shell").Run url

相關問題