【问题标题】:Open Google Chrome from VBA/Excel从 VBA/Excel 打开谷歌浏览器
【发布时间】:2021-03-22 14:02:17
【问题描述】:

我正在尝试从 VBA 打开 Chrome 浏览器。我了解 Chrome 不支持 ActiveX 设置,所以我想知道是否有任何解决方法?

Dim ie As Object 
Set ie = CreateObject("ChromeTab.ChromeFrame")
ie.Navigate "google.ca" 
ie.Visible = True

【问题讨论】:

  • Chrome 可以是标准浏览器吗,或者即使不是,您也需要打开 Chrome?如果是第一种情况,我猜你可以使用 ShellExecute 和 URL 之类的东西。

标签: vba google-chrome excel


【解决方案1】:
shell("C:\Users\USERNAME\AppData\Local\Google\Chrome\Application\Chrome.exe -url http:google.ca")

【讨论】:

  • 嗨@ray,如果它是一个动态网站怎么办。我尝试了 Shell(chromePath 和“-url”和网站地址)。但是页面加载失败,只加载了google的主页。
  • 您可以与应用程序交互吗?例如,如果我想对返回的 HTML 进行一些分析?
  • 我确定您的意思是这样 - 但用户需要在此代码生效之前找出当前用户是谁。您可以使用:current_user = (Environ$("Username")) 然后执行 ChromePath = """c:\users\" & current_user & "\AppData\Local\Google\Chrome\Application\Chrome.exe"""
  • @user 我知道它已经晚了,但我正在搜索相同的内容并发现 Shell ("C:\Users\DELL\AppData\Local\Google\Chrome\Application\Chrome.exe -url " & URL) 为我工作(-url 后面有一个空格)
  • 要添加到这个答案,我的应用程序在这里找到:C:\Program Files (x86)\Google\Chrome\Application\chrome.exe。为了找到它,我右键单击应用程序图标并单击“属性”。这告诉我快捷方式图标指向的位置。我希望这对正在寻找 Chrome.exe 所在位置的其他人有所帮助。
【解决方案2】:

也在这里工作过:

Sub test544()

  Dim chromePath As String

  chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""

  Shell (chromePath & " -url http:google.ca")

End Sub

【讨论】:

  • 嗨@ahmad,如果它是一个动态网站怎么办。我尝试了 Shell(chromePath 和“-url”和网站地址)。但是页面加载失败,只加载了google的主页。
【解决方案3】:

我找到了一种更简单的方法,即使您不知道 chrome 所在的路径,它也能完美运行。

首先,您必须将此代码粘贴到模块顶部。

Option Explicit
Private pWebAddress As String
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

之后你必须创建这两个模块:

Sub LoadExplorer()
    LoadFile "Chrome.exe" ' Here you are executing the chrome. exe
End Sub

Sub LoadFile(FileName As String)
    ShellExecute 0, "Open", FileName, "http://test.123", "", 1 ' You can change the URL.
End Sub

有了这个,您将能够(如果您愿意)为 url 设置一个变量,或者像硬编码一样保留它。

Ps:对于其他浏览器,只需将“Chrome.exe”更改为opera、bing等即可。

【讨论】:

    【解决方案4】:

    您可以使用以下 vba 代码并将它们输入到 excel 的标准模块中。可以输入网站列表,应该在 Excel 中的单元格 A1 上像这样输入 - www.stackoverflow.com

    ActiveSheet.Cells(1,2).Value 仅获取您在 Excel 中单元格 B1 上拥有的网站链接数,并将根据您放置在工作表上的网站链接数一次又一次地循环代码。因此,Chrome 将为每个网站链接打开一个新标签页。

    我希望这对您拥有的动态网站有所帮助。

    Sub multiplechrome()
    
        Dim WebUrl As String
        Dim i As Integer
    
        For i = 1 To ActiveSheet.Cells(1, 2).Value
            WebUrl = "http://" & Cells(i, 1).Value & """"
            Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url " & WebUrl)
    
        Next
    End Sub
    

    【讨论】:

      【解决方案5】:

      上面@ray 给出的答案完美无缺,但请确保您使用正确的路径打开文件。如果您右键单击图标并单击属性,您应该会看到实际路径在哪里,只需复制过去即可。

      【讨论】:

        【解决方案6】:

        您可以使用 selenium basic 启动 Chrome 并与 Chrome 交互。安装后,您需要添加对 Selenium 类型库的引用。

        Option Explicit
        Public Sub Demo()
            Dim d As WebDriver
            Set d = New ChromeDriver
            Const URL = "https://www.google.com/"
        
            With d
                .Start "Chrome"
                .get URL
                .FindElementById("lst-ib").SendKeys "Selenium basic GitHub"
                 .FindElementsByTag("form")(1).FindElementByCss("input[value='Google Search']").Click
                '.Quit
            End With
        End Sub
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-04-20
          • 1970-01-01
          • 1970-01-01
          • 2018-12-07
          • 2019-09-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多