Solved

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

Posted on 2013-11-24
10
2,096 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 80

Expert Comment

by:byundt
Comment Utility
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
Comment Utility
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
Comment Utility
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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
perfect Thanks...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

743 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

19 Experts available now in Live!

Get 1:1 Help Now