• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2455
  • Last Modified:

vba code from excel opening webbrowser but keeps opening new ie windows

excel vba ...this code keeps opening up a new IE evertime i execute.

Just need to activate one browser and keep refreshing.
If i forget to close the IE  window.


Dim br As WebBrowser
 Set br = CreateObject("InternetExplorer.Application")
     
    
   Set br = CreateObject("InternetExplorer.Application")
    
   With br
  
  
  ' Grainger open search
  
  br.Navigate ("http://www.google.com)
        
     
    .StatusBar = True
    .Toolbar = True
    .Visible = True
    .Resizable = True
    .AddressBar = True
    
    End With
  'Wait until page is loaded
    Do While br.Busy
        DoEvents
    Loop

 Set br = Nothing

Open in new window



Thanks
fordraiders
0
Fordraiders
Asked:
Fordraiders
  • 4
  • 3
  • 3
1 Solution
 
yuppyduCommented:
How do you execute the code? This code initializes IE, therefore everytime you launch it it will open a new browser. Can you better explain what you are trying to do?
0
 
yuppyduCommented:
Try this, it will refresh every 5 seconds

Sub Browser()

 Set br = CreateObject("InternetExplorer.Application")
     
   
   Set br = CreateObject("InternetExplorer.Application")
   
   With br
 
 
  ' Grainger open search
 
  br.Navigate ("http://www.google.com")
       
     
    .StatusBar = True
    .Toolbar = True
    .Visible = True
    .Resizable = True
    .AddressBar = True
   
    End With
  'Wait until page is loaded
    Do While br.Busy
        DoEvents
    Loop
Do
 
 Application.Wait Now + TimeValue("00:00:05")
br.Refresh
Loop
 Set br = Nothing
End Sub
0
 
yuppyduCommented:
it will not exit until you don't Ctrl+Break the code. If you wish you can add a counter which will stop the code after a certain number of loops
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
byundtCommented:
If you want to capture an existing instance of Internet Explorer, you might think it logical to use:
Dim IE As Object
Set IE = GetObject(, "InternetExplorer.Application")

Open in new window

Unfortunately, GetObject doesn't work as it always returns an error result.

As a workaround, you can loop through the open windows in a Shell instance, searching for one displaying HTML:
Function GetIE() As Object
Dim IE As Object, oShApp As Object, oWin As Object
Set oShApp = CreateObject("Shell.Application")
For Each oWin In oShApp.Windows
    If TypeName(oWin.Document) = "HTMLDocument" Then
        Set IE = oWin
        Set oShApp = Nothing
        Set oWin = Nothing
        Exit For
    End If
Next
If IE Is Nothing Then
    Set IE = CreateObject("InternetExplorer.Application")
End If
Set GetIE = IE
End Function

Sub test()
Dim IE As Object
Set IE = GetIE()
MsgBox IE.LocationURL
End Sub

Open in new window

0
 
FordraidersAuthor Commented:
eventually, I will have a textbox on my form.

t =  useform2.textbox1.text

I was going to pass a variable into a url...when a person wanted to intiate a search.,

I just did not want a new webbrowser to open everyitme they did a search.

br.Navigate ("http://www.fastenal.com/web/products/detail.ex?sku=" & t & " ")

Thanks..
Sorry i did not explain better.
fordraiders
0
 
byundtCommented:
I grabbed an SKU from the Fastenal web site for a M3 aluminum fastener. The following code will open that web page. Note that all I changed from the preceding code is in the test sub; the function GetIE was left unchanged.
Function GetIE() As Object
Dim IE As Object, oShApp As Object, oWin As Object
Set oShApp = CreateObject("Shell.Application")
For Each oWin In oShApp.Windows
    If TypeName(oWin.Document) = "HTMLDocument" Then
        Set IE = oWin
        Set oShApp = Nothing
        Set oWin = Nothing
        Exit For
    End If
Next
If IE Is Nothing Then
    Set IE = CreateObject("InternetExplorer.Application")
End If
Set GetIE = IE
End Function

Sub test()
Dim IE As Object
Dim t As String
Set IE = GetIE()
t = "11509957"
IE.Navigate ("http://www.fastenal.com/web/products/detail.ex?sku=" & t & " ")
End Sub

Open in new window

0
 
FordraidersAuthor Commented:
byundt, Thanks..just to clarify...This will keep just one web browser open per each search initiated on a different search ?
0
 
FordraidersAuthor Commented:
the old code...if i changed th sku number number and initiated a new search say 5 times...i have 5 webbrowsers open...eventaully.
0
 
byundtCommented:
My code will capture an existing instance of Internet Explorer if one is already open. If not, it will open Internet Explorer for you. It will then return an object variable pointing to Internet Explorer. What you do with that object variable is up to you.

The following sub uses GetIE and will navigate to 3 successive web pages on the Fastenal web site. If you run the macro from the ALT + F8 prompt, you will get an error message on the second SKU. I believe the error is associated with time needed to load a web page, because if you step through the macro one instruction at a time, everything works perfectly. As the macro runs, the first tab in Internet Explorer successively loads three different Fastenal web pages.
Sub test()
Dim IE As Object
Dim v As Variant
Dim t As String
Set IE = GetIE()
For Each v In Array("11509957", "11509917", "11509920")
    t = CStr(v)
    IE.Navigate ("http://www.fastenal.com/web/products/detail.ex?sku=" & t & " ")
Next
End Sub

Open in new window

0
 
FordraidersAuthor Commented:
perfect Thanks...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now