Link to home
Start Free TrialLog in
Avatar of jimtxas
jimtxas

asked on

Open URL in existing IE window VBScript

I have a script that opens a URL in IE (or default browser) via a vb script from my application.  This script will dynamically pass certain pages.  It works fine but it opens a new IE instance each time.  If IE is already open, i just want to navigate to the new URL.  I have tried all of the window style options but get the same behavior on each.

Below is the core of the code-

Dim wShell
Set wShell = CreateObject("WScript.Shell")
WshShell.Run "http://www.google.com/", 9


Any ideas?

Tks,
J

Avatar of mvidas
mvidas
Flag of United States of America image

Hi J,

The way you have it is nice as it will always use the default browser.  However, if you want it to use existing windows I believe you would have to use IE automation, like:

 Dim IE, URL
 URL = "https://www.experts-exchange.com/questions/22023470/Open-URL-in-existing-IE-window-VBScript.html"
 Set IE = Nothing
 On Error Resume Next
 Set IE = GetObject(, "internetexplorer.application")
 On Error GoTo 0
 If IE Is Nothing Then
  Set IE = CreateObject("internetexplorer.application")
  IE.Visible = True
 End If
 IE.Navigate2 URL

That will look for any open IE windows, if it finds one then it will use that otherwise it will create a new one.  It will force the user to use IE though.

Matt
Avatar of jimtxas
jimtxas

ASKER

Forcing IE would be fine, however your example still opens in a new window each time...
It shouldn't, it always works for me.  You removed the previous Shell code (or pasted this into a new .vbs file)?
I just tried it again and it works as it should. After the check if IE is nothing, try adding a message box to verify, like:

 If IE Is Nothing Then
  MsgBox "IE not open"
'rest of code

If so, I don't know what to tell you, thats the best way to do it that I'm aware of.
Matt
Avatar of jimtxas

ASKER

I made one minor change in the Set IE GetObject below which stops it from opening a new window...  however if the window is open, it doesn't navigate correctly...

Any Ideas?

Dim IE, URL
 URL = "https://www.experts-exchange.com/questions/22023470/Open-URL-in-existing-IE-window-VBScript.html"
 Set IE = Nothing
 On Error Resume Next
 Set IE = GetObject("", "internetexplorer.application")
 On Error GoTo 0
 If IE Is Nothing Then
  Set IE = CreateObject("internetexplorer.application")
  IE.Visible = True
 End If
 IE.Navigate2 URL
You said it doesnt navigate correctly, what is it doing?
What versions (OS, IE) are you using?

You could try using
 IE.Navigate URL
instead of Navigate2. The difference is that navigate2 allows you to use things you couldnt use in navigate (shell integration); I always use navigate2 and have never had an issue, but perhaps it will make a difference for you.
Avatar of jimtxas

ASKER

XP, IE6.0

It seems to get the object but the method doesn't fire... ive tried navigate, navigate2, Quit, gosearch, refresh and it does nothing in the IE window...
Hmmm.. I'm on win2k and ie6 and it works fine for me, I can't imagine why it would be different in XP but apparently it is.  Hopefully someone with XP will comment soon on it.
What you may want to do is go to www.ee-stuff.com and create a Pointer question in the Visual Basic topic area to this question.
ASKER CERTIFIED SOLUTION
Avatar of dave_moats
dave_moats

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimtxas

ASKER


Worked like a charm-

You're my  HERO!
it is not working for me.  I am using IE8 on Windows 7.  I want to use the existing IE instance and existing tab.  But following code is opening new IE window.

  WScript.Quit Main
  Function Main
  Set IE = GetObject("", "internetexplorer.application")
  IE.Visible = True
  IE.Navigate2 "http://www.msn.com"
      Do While IE.readystate <> 4  
      Loop  
 
  End Function
  WScript.Quit Main
  Function Main
  Set IE = GetObject("", "internetexplorer.application")
  IE.Visible = True
  IE.Navigate2 "http://www.msn.com"
      Do While IE.readystate <> 4  
      Loop  
 
  End Function

Open in new window

Hi,

The above solution is not working for me. Could you please help. I am working on Win7 using IE8.

the below code opens the new browser every time :

 Dim IE, URL
 URL = "https://www.experts-exchange.com/questions/22023470/Open-URL-in-existing-IE-window-VBScript.html"
 Set IE = Nothing
 On Error Resume Next
 Set IE = GetObject(, "internetexplorer.application")
 On Error GoTo 0
 If IE Is Nothing Then
  Set IE = CreateObject("internetexplorer.application")
  IE.Visible = True
 End If
 IE.Navigate2 URL


and this one is not working for me in QTP
on error resume next

 Set objShell = CreateObject("Shell.Application")
 Set objShellWindows = objShell.Windows
 Set IE = objShellWindows.Item

 URL = "https://www.experts-exchange.com/questions/22023470/Open-URL-in-existing-IE-window-VBScript.html"

 IE.Navigate2 URL

 If err.number <> 0 Then
  Set IE = wscript.CreateObject("internetexplorer.application")
  IE.Visible = True
  IE.Navigate2 URL
 End If

 Set IE = Nothing
 Set objShellWindows = Nothing
 Set objShell = Nothing

[END SAMPLE]

Pleae help me out.