Solved

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

Posted on 2013-11-24
10
2,124 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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 …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now