VBScript To Launch Internet Explorer With A URL Generated

南楼画角 提交于 2020-06-29 07:41:53

问题


Currently I'm a learning HTA programming. I have a requirement as follows.

There are two input boxes in the form (both are mandatory). When I enter the values and click on search button, a url would be created based on the inputs. The should launch the Internet Explorer application with the generated url.

My issue is that I'm able to launch IE browser but I'm not able to pass the url to it. I have tried many ways but I'm not able to get it done.

I have the given my code below. I have removed my erroneous syntax of passing the url to the browser. The below code would create the url and launch a blank IE explorer.

Please help me. Thanks in advance.

<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>

回答1:


You can load a URL in the default browser by using the Run() method of WShell:

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

If you want to explicitly load the URL in Internet Explorer, you'll need to determine the full path to IEXPLORE.EXE first, then pass it to Run():

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

Or

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

Note the quotes. You'll want to put quotes around the path to IE in case the path contains a space. In order to specify a quote within a string, you have to double it. If the quotes are confusing, you can use Chr(34) to specify a quote character:

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



回答2:


To make life easier, i usually use this function to add the double quotes in a variable.

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

An example showing you how it is easy to use it.

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
'****************************************************************************************************



回答3:


Give it a try with this 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>



回答4:


The below code snippet would launch this url in the default browser.

CreateObject("WScript.Shell").Run url



来源:https://stackoverflow.com/questions/21802484/vbscript-to-launch-internet-explorer-with-a-url-generated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!