Geographos
Geographos

Reputation: 1456

Open multiple URLs in the Chrome browser

I would like to setup my macro with an option of opening a few websites in the Google Chrome browser.

The advice, which I found come from these links:

Parse webpage through VBA with Chrome and https://www.reddit.com/r/excel/comments/48yikv/using_vba_to_launch_chrome_and_log_in/

I also found some solution here:

https://www.devhut.net/2018/02/01/vba-open-a-url-in-firefox-chrome/

but it refers mostly to choice of options between the few browsers, which I don't want.

My code so far looks like this:

 Sub Websites()
 Dim Chrome As Object
 Dim url1, url2, url3, url4, url5 As String
 Dim postcode1, postcode2, postcode3 As String

 postcode1 = Sheets("Frontsheet").Range("AA2").Value
 postcode2 = Sheets("Frontsheet").Range("AA3").Value

 Dim Location

 url1 = "https://www.google.com/maps/place/+" & postcode1 & "+" & postcode2
 url2 = "https://www.openreach.co.uk/ormaps/pia/v2/"
 url3 = "https://historicengland.org.uk/listing/the-list/map-search?postcode=" & postcode1
 url4 = "https://www.nhs.uk/service-search/other-services/UrgentCare/UrgentCareFinder? 
Location.Id=0&Location.Name=" & postcode1
 url5 = "https://www.mapping.cityoflondon.gov.uk/geocortex/mapping/?viewer=compass&runworkflowbyid=Switch_layer_themes&LayerTheme=Show%20the%20Explore%20The%20City%20layers"

 Set Chrome = "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url"

 With Chrome
    .Visible = True
    .Navigate url1
    .Navigate url2, CLng(2048)
    .Navigate url3, CLng(2048)
    .Navigate url4, CLng(2048)
    .Navigate url5, CLng(2048)
    .Top = 5
    .Left = 5
    .Height = 1300
    .Width = 1900

  End Sub

Unfortunately, I am getting error:

Type mismatch

which refers to the following line of code:

   Set Chrome = "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url"

It appears even when I take down the -url statement.

I tried also something like this:

 Set Chrome = "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe" & URL

But now the error states fairly:

Variable not defined

because I set variables url1-url4

How can I parse all of them then?

Upvotes: 1

Views: 1387

Answers (1)

CLR
CLR

Reputation: 12279

As BigBen points out with a link, Chrome can't be used in object sense in the same way that I.E. can. But you can request it open URLs like so:

Sub Test

 Dim postcode1 as String, postcode2 As String
 Dim url1 As String, url2 As String, url3 As String, url4 As String, url5 As String

 postcode1 = Sheets("Frontsheet").Range("AA2").Value
 postcode2 = Sheets("Frontsheet").Range("AA3").Value

 url1 = "https://www.google.com/maps/place/+" & postcode1 & "+" & postcode2
 url2 = "https://www.openreach.co.uk/ormaps/pia/v2/"
 url3 = "https://historicengland.org.uk/listing/the-list/map-search?postcode=" & postcode1
 url4 = "https://www.nhs.uk/service-search/other-services/UrgentCare/UrgentCareFinder?Location.Id=0&Location.Name=" & postcode1
 url5 = "https://www.mapping.cityoflondon.gov.uk/geocortex/mapping/?viewer=compass&runworkflowbyid=Switch_layer_themes&LayerTheme=Show%20the%20Explore%20The%20City%20layers"

 OpenChrome url1
 OpenChrome url2
 OpenChrome url3
 OpenChrome url4
 OpenChrome url5

End Sub

Sub OpenChrome(url As String)
    Dim chrome As String
    chrome = "C:\Program Files (x86)\Google\Chrome\Application\Chrome.exe -url"
    Shell (chrome & " " & url)
End Sub

PS. You need to Dim each type e.g. String etc. separately. You can't list them all and then define the type.

Upvotes: 2

Related Questions