7

I'm trying to write a bit of VBScript to open a browser on a specific webpage. Ultimately this webpage would be unique per script. At the moment I have the following code which works:

Dim objShell

objShell = CreateObject("Shell.Application")
objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "www.google.ie", "", "", 1)

But I would like to get the following working:

Dim iURL As String
Dim objShell

iURL = "www.google.ie"

objShell = CreateObject("Shell.Application")
objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", iURL, "", "", 1)

Any ideas what it is I'm doing wrong? Any help would be greatly appreciated.

1
  • Found out the problem was being caused in the external program launching the VBScript. Thanks for your help Alex K. Commented Nov 16, 2012 at 9:52

4 Answers 4

17

No As String as VBScript is not strongly type, you need a set when you create an instance of the COM object & no parens around the method call;

Dim iURL 
Dim objShell

iURL = "www.google.ie"

set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "chrome.exe", iURL, "", "", 1

Or if chrome is the default

set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm using visual studio express 2012 for web and it keeps removing set and adding back in the parenthesis. I tried everything else the same way you recommend, but am getting the same result. The variable is not being used, but a direct string will work.
As a improvement you could use: oShell.ShellExecute "chrome.exe", """" & iURL & """", "", "", 1 (will allow to pass parameters splitted by space without encoding it)
Thank you for sharing... OneDrive deprecated Windows 7 share.... and I need to write a simple vbs script for it... M$ is bad.. and Windows 7 ESU still deprecated in 2026... why would they drop the support *smh...
2

I found the easiest way is to do this:

set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.run "chrome.exe"
WScript.sleep 400
WshShell.sendkeys "URL HERE"
WshShell.sendkeys "{ENTER}"

also just a fyi you can do this to close chrome:

WshShell.sendkeys "%{F4}"

1 Comment

hopefully i didn't make any typos
1

Can you pl insert "Call" prefix to "objShell.ShellExecute"

Dim objShell
Set objShell = CreateObject("Shell.Application")

iURL = "www.google.com"

Call objShell.ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", iURL, "", "", 1)

For IE:

'Call objShell.ShellExecute("iexplore.exe", iURL, "", "", 1)


For more info below code also works,

Dim objShell
Set objShell = CreateObject("Shell.Application")

chrome:

iURL = "www.google.com"
'objShell.ShellExecute "chrome.exe", iURL, "", "", 1

ie:

'objShell.ShellExecute "iexplore.exe", iURL, "", "", 1

Comments

1
  Sub RickRoller()
   Dim counter, myNum, objShell, iURL
   counter = 0
   myNum = 100
   Do Until myNum = 1
     myNum = myNum - 1
     counter = counter + 1
     Set WshShell = CreateObject("WScript.Shell")
     WshShell.SendKeys(chr(&hAF))
   Loop
   set objShell = CreateObject("WScript.Shell")
   iURL = "https://youtu.be/oHg5SJYRHA0?autoplay=1&t=44"
   objShell.run(iURL)
 End Sub


 RickRoller()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.