Search code examples
internet-explorervbscriptscriptinghta

VBScript To Launch Internet Explorer With A URL Generated


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>

Solution

  • 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