Search code examples
excelvbauserform

Search for city on Google Maps inside Excel Userform on Initialize


I am wondering if it possible to search for specific city on Google Map inside Excel userform once Userform is opened?

Userform has a name Flights. It has Web browser with name WebBrowser1

Here is my current code:

Private Sub UserForm_Initialize()

    Flights.WebBrowser1.Navigate "https://maps.google.com"

End Sub

I have tried:

Private Sub UserForm_Initialize()

    Flights.WebBrowser1.Navigate "https://maps.google.com"
    Flights.WebBrowser1.Document.getElementsByName("q")(0).Value = "Washington"
    Flights.WebBrowser1.Document.getElementsByClassName("searchbox-searchbutton")(0).Click

End Sub

Solution

  • Just navigate to https://www.google.com/maps?q=washington or https://maps.google.com?q=washington.

    Private Sub UserForm_Initialize()
    
        Flights.WebBrowser1.Navigate "https://maps.google.com?q=washington"
    
    End Sub
    

    alternatively you could try (not tested)

    With Flights.WebBrowser1
        .Navigate "https://maps.google.com"
        Do While .readyState <> 4 Or .Busy: DoEvents: Loop 'wait until page loads
        .Document.getElementsByName("q")(0).Value = "Washington"
        .Document.getElementsByID("searchbox_form")(0).submit
    End With