Solved

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

Posted on 2013-11-24
10
2,150 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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