Code error when opened Browser from Textbox

Sharid

Well-known Member
Joined
Apr 22, 2007
Messages
1,064
Office Version
  1. 2016
Platform
  1. Windows
Hi
I am opening a url from a textbox. So when then button is clicked, the IE will open and display whichever url is in the textbox. The code works fine only the first time. If I close the IE and then add a new url into the textbox. When I click the button again I get the first error message on the code show below. To resolve this, I then added a bit more code but the error just moved to another part of the code. I can not seem to work this out

VBA Code:
Private Sub UrlsBrowserOpenBt_click()
'urls browser button
'This search will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    
    'Create InternetExplorer Object
         Set IE = CreateObject("InternetExplorer.Application")

    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True
 
    'Define URL
        URL = [B]UrlsSearchTextBox1.Value[/B]
 
    'Navigate to URL
        IE.navigate URL
 
    ' Statusbar let's user know website is loading
          Application.StatusBar = URL & " is loading. Please wait..."
 
    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
          Do While IE.readyState = 4: DoEvents: Loop   'Do While
          Do Until IE.readyState = 4: DoEvents: Loop   'Do Until
 
    'Webpage Loaded
         Application.StatusBar = URL & " Loaded"
    
    'Unload IE
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
    
End Sub

First Error Message
1593262553240.png

Error on this line of code
1593262597039.png


I added this bit of code, hoping it would get rid on the error
VBA Code:
 On Error Resume Next
         Set IE = CreateObject("InternetExplorer.Application")
    On Error GoTo 0

I now get this error message on the code below.
1593262052678.png


1593262246821.png


thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
This works perfectly for me every time

VBA Code:
    Dim URL As String, IE
    On Error Resume Next
    Set IE = CreateObject("InternetExplorer.Application")
    IE.Visible = True
    URL = TextBox1.Text
    IE.navigate URL
    On Error GoTo 0
    Set IE = Nothing
 
Upvote 0
Yongle

Thanks for for code, but its a bit hit and miss. Some time it works and other times I get the same error message.

I use another code to search for urls. Although it closes the IE browser, could it be that it is not clearing some thing correctly and therefor when I run the code to search the url, it conflicts and that is why there some hit and miss with the code, as it opens some urls and not others and sometime give error message and other times its fine.

This is how the IE is closed in my first code, which searches for urls.
VBA Code:
IE.Quit
    Set IE = Nothing
    Set HTMLdoc = Nothing
    Set nextPageElements = Nothing
    Set li = Nothing
    Set link = Nothing

Could this be conflicting and not closing the browser correct so when I run the second code I posting in my first post there is some conflict with the IE. As sometime it works fine and other times it give me an error message.

Also I did use, this code in my code and got and error message, so i commented out the "On Error GoTo 0" and this issue went away for the most part. Some urls would open in the brower and some would not.

VBA Code:
On Error Resume Next
         Set IE = CreateObject("InternetExplorer.Application")
    On Error GoTo 0

With your code, I get the above "object variable error message" NOW AT the ready state part of the code.
 
Upvote 0
Ignore my code, which is not doing anything different to your code

Try this - your code with 1 line added
Rich (BB code):
Private Sub UrlsBrowserOpenBt_click()
    Call IE_Sledgehammer
'urls browser button
'This search will load a webpage in IE
    Dim i As Long
    Dim URL As String
    Dim IE As Object
    Dim objElement As Object
    Dim objCollection As Object
    'Create InternetExplorer Object
         Set IE = CreateObject("InternetExplorer.Application")
    'Set IE.Visible = True to make IE visible, or False for IE to run in the background
    IE.Visible = True
    'Define URL
        URL = UrlsSearchTextBox1.Value
    'Navigate to URL
        IE.navigate URL
    ' Statusbar let's user know website is loading
          Application.StatusBar = URL & " is loading. Please wait..."
    ' Wait while IE loading...
    'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
          Do While IE.readyState = 4: DoEvents: Loop   'Do While
          Do Until IE.readyState = 4: DoEvents: Loop   'Do Until
    'Webpage Loaded
         Application.StatusBar = URL & " Loaded"
    'Unload IE
    Set IE = Nothing
    Set objElement = Nothing
    Set objCollection = Nothing
End Sub

credit : Close all IE Browser windows with VBA
Rich (BB code):
Sub IE_Sledgehammer()
    On Error Resume Next
    Dim objWMI As Object, objProcess As Object, objProcesses As Object
    Set objWMI = GetObject("winmgmts://.")
    Set objProcesses = objWMI.ExecQuery( _
        "SELECT * FROM Win32_Process WHERE Name = 'iexplore.exe'")
    For Each objProcess In objProcesses
        Call objProcess.Terminate
    Next
    Set objProcesses = Nothing: Set objWMI = Nothing
    On Error GoTo 0
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,764
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top