Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control Internet explorer from excel

Status
Not open for further replies.

bartrein

Programmer
Sep 22, 2008
49
0
0
EU
Hi Everyone

I have a little macro which opens internet explorer and finds the relevant website using data from my spreadsheet.

It works fine however the problem is that each time you run this macro it creates a new instance of IE object i.e. opens new window. There are times that you have to run it several times and you end up with say 20 IE windows.
To keep it tidy i would like to incorporate into my code a statement that would say something like this :

check if internet explorer is already opened
- if not - create a new instance and so on,
- or if it is already open use the active window and navigate to my new url from there.

Can anyone help?

-----------------------------------------------------------

Dim appIE As Object
Dim sURL As String

If Right(ActiveCell.Text, 2) = "CZ" Then sURL = " & Left(ActiveCell.Text, InStr(1, ActiveCell.Text, " ") - 3) & "-" & Left(Right(Left(ActiveCell.Text, InStr(1, ActiveCell.Text, " ") - 1), 2), 1) & "&x=10&y=7"

Set appIE = New InternetExplorer

With appIE
.Navigate sURL
Do Until appIE.ReadyState = READYSTATE_COMPLETE
Loop
.Visible = True
End With
 
Have a look at GetObject and CreateObject:
Code:
On Error Resume Next
Set appIE = GetObject(, "InternetExplorer.Application")
If Err <> 0 Then
  Set appIE = CreateObject("InternetExplorer.Application")
End If
With appIE
...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV, what do i use as a path argument?
 
You could use webbrowser control on the userform or pass the IE to WitEvents declared variable. In both cases you will be able to pick Quit or NavigateComplete events.

combo
 
what do i use as a path argument[/!]
???
Simply replace your Set appIE = New InternetExplorer line of code with the top 5 lines of my suggestion.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks but even after using your 5 lines instead of Set appIE = New InternetExplorer - it still opens a new window instead of using the exisitng one.
 
That is because
Set appIE = GetObject(, "InternetExplorer.Application")
always raises error even if internet is open.
That's way i was asking for the "path"-first argument of this method before the comma.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top