Solved

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

Posted on 2013-11-24
10
2,266 Views
Last Modified: 2013-11-24
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
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 4

Expert Comment

by:yuppydu
ID: 39672943
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
 
LVL 4

Expert Comment

by:yuppydu
ID: 39672977
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
 
LVL 4

Expert Comment

by:yuppydu
ID: 39672979
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
Independent Software Vendors: 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!

 
LVL 81

Expert Comment

by:byundt
ID: 39673112
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
 
LVL 3

Author Comment

by:fordraiders
ID: 39673346
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
 
LVL 81

Expert Comment

by:byundt
ID: 39673352
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
 
LVL 3

Author Comment

by:fordraiders
ID: 39673362
byundt, Thanks..just to clarify...This will keep just one web browser open per each search initiated on a different search ?
0
 
LVL 3

Author Comment

by:fordraiders
ID: 39673369
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
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39673381
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
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 39673479
perfect Thanks...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question