首页 > 解决方案 > 在 Chrome 浏览器中打开多个 URL

问题描述

我想通过在 Google Chrome 浏览器中打开几个网站的选项来设置我的宏。

我发现的建议来自以下链接:

使用 Chrome和 https://www.reddit.com/r/excel/comments/48yikv/using_vba_to_launch_chrome_and_log_in/通过 VBA 解析网页

我还在这里找到了一些解决方案:

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

但它主要是指在少数浏览器之间选择选项,这是我不想要的。

到目前为止,我的代码如下所示:

 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

不幸的是,我收到错误:

类型不匹配

它指的是以下代码行:

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

即使我删除了-url声明,它也会出现。

我也尝试过这样的事情:

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

但现在错误状态相当不错:

变量未定义

因为我设置了变量 url1-url4

那我该如何解析所有这些?

标签: excelvba

解决方案


正如 BigBen 通过链接指出的那样,Chrome 不能像 IE 那样在对象意义上使用。但是您可以像这样请求它打开 URL:

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。您需要分别对Dim每种类型,例如String等。您不能将它们全部列出然后定义类型。


推荐阅读