Good evening all.

I found this (example) code which should access Wikipedia in Internet Explorer), put some data in a box and press the search button. All works well until it tries to press the "Go" button. I suspect this may be old code and the button is no longer called "Go" but "searchButton".

Anyway, two problems; if I leave it at "Go", I get an error: Run-time error '424', Object required. I thought, ah, I can cope with this, and edited it to "searchButton". This stops the error message but doesn't input the search term, it goes to the Search page as if I have cleared the search term (Document Object Model) ... so that's the second problem.

Note: it requires references to Microsoft HTML Object Library and Microsoft Internet Controls.


I would be grateful if someone could help me make this example work so that I can then adapt it to do other things. Alternatively, if you can provide code that does the same thing but in a different way, that would make me very happy too. Or, even better, a solution that isn't dependent on IE at all, but can use the default browser ... but I guess that could be asking a lot.

Thanks in advance, TMS

Sub ExplorerTest()
Const myPageTitle As String = "Wikipedia"
Const myPageURL As String = "http://en.wikipedia.org/wiki/Main_Page"
Const mySearchForm As String = "searchform"
Const mySearchInput As String = "searchInput"
Const mySearchTerm As String = "Document Object Model"
Const myButton As String = "Go"
'Const myButton As String = "searchButton"

Dim myIE As SHDocVw.InternetExplorer

'check if page is already open
Set myIE = GetOpenIEByTitle(myPageTitle, False)

If myIE Is Nothing Then
    'page isn't open yet
    'create new IE instance
    Set myIE = GetNewIE
    'make IE window visible
    myIE.Visible = True
    'load page
    If LoadWebPage(myIE, myPageURL) = False Then
        'page wasn't loaded
        MsgBox "Couldn't open page"
        Exit Sub
    End If
End If
  
With myIE.Document.forms(mySearchForm)
    'enter search term in text field
    .elements(mySearchInput).Value = mySearchTerm
    'press button "Go"
    .elements(myButton).Click
End With
       
End Sub

There are several functions to support this worked example:

Option Explicit

' Before you can do anything with the Internet Explorer,
' of course you'll need one, and you'll need something to address it. The following function achieves exactly this by creating a new instance of the Internet Explorer and returning a pointer to it.

'returns new instance of Internet Explorer
Function GetNewIE() As SHDocVw.InternetExplorer
    'create new IE instance
    Set GetNewIE = New SHDocVw.InternetExplorer
    'start with a blank page
    GetNewIE.Navigate2 "about:Blank"
End Function

' The next function loads a webpage:
' loads a web page and returns True or False depending on
' whether the page could be loaded or not
Function LoadWebPage(i_IE As SHDocVw.InternetExplorer, _
                     i_URL As String) As Boolean
With i_IE
    'open page
    .Navigate i_URL
    'wait until IE finished loading the page
    Do While .ReadyState <> READYSTATE_COMPLETE
        Application.Wait Now + TimeValue("0:00:01")
    Loop
    'check if page could be loaded
    If .Document.URL = i_URL Then
        LoadWebPage = True
    End If
End With
End Function

' But what if the desired page has already been loaded
' and you just want to use it?
' The following function returns a pointer to an already
' open instance of the Internet Explorer, if it has the
' desired page loaded. It'll find it by comparing this
' page's URL with the URLs of open pages in the Internet Explorer.
' If the page could not be found, the function returns Nothing.

' finds an open IE site by checking the URL
Function GetOpenIEByURL(ByVal i_URL As String) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

'ignore errors when accessing the document property
On Error Resume Next
'loop over all Shell-Windows
For Each GetOpenIEByURL In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByURL.Document) = "HTMLDocument" Then
        'check the URL
        If GetOpenIEByURL.Document.URL = i_URL Then
            'leave, we found the right window
            Exit Function
        End If
    End If
Next
End Function

' Sometimes, when you enter an URL to load a page,
' you'll get redirected, and thus the URL changes.
' Or your site uses frames and has the same URL for
' different content, so you can't clearly identify a
' page by its URL. Then you could check the title of
' the page instead:

'finds an open IE site by checking the title
Function GetOpenIEByTitle(i_Title As String, _
                          Optional ByVal i_ExactMatch As Boolean = True) As SHDocVw.InternetExplorer
Dim objShellWindows As New SHDocVw.ShellWindows

If i_ExactMatch = False Then i_Title = "*" & i_Title & "*"
'ignore errors when accessing the document property
On Error Resume Next
'loop over all Shell-Windows
For Each GetOpenIEByTitle In objShellWindows
    'if the document is of type HTMLDocument, it is an IE window
    If TypeName(GetOpenIEByTitle.Document) = "HTMLDocument" Then
        'check the title
        If GetOpenIEByTitle.Document.Title Like i_Title Then
            'leave, we found the right window
            Exit Function
        End If
    End If
Next
End Function