【问题标题】:Connecting Edge / Chrome to VBA将 Edge / Chrome 连接到 VBA
【发布时间】:2022-04-22 17:06:02
【问题描述】:

我有一个 VBA 代码,我一直用它来打开 Internet Explorer,复制特定网站上的信息,然后粘贴到单元格中。现在的问题是该网站现在不再使用 IE。

我正在尝试调整此代码以将其与 Edge 和/或 Chrome 一起使用(我已经安装了 Selenium),但实际上我正在努力解决它。

有人可以帮我调整一下代码吗?

Option Explicit


Sub Test()


    Dim IE As Object
    On Error Resume Next
    Application.DisplayAlerts = False
    
    Sheets("Sheet3").Select
    Range("A1:A1000") = "" ' erase previous data
    Range("A1").Select


    Set IE = CreateObject("InternetExplorer.Application")
        With IE
            .Visible = True
            .Navigate "https://google.com" ' should work for any URL
            Do Until .ReadyState = 4: DoEvents: Loop
        End With


        IE.ExecWB 17, 0 '// SelectAll
        IE.ExecWB 12, 2 '// Copy selection
        ActiveSheet.PasteSpecial Format:="Text", link:=False, DisplayAsIcon:=False
        Range("A1").Select
        IE.Quit

    Application.DisplayAlerts = True
End Sub

如何将 IE 提及切换到 Edge 或 Chrome 提及?比如把IE换成objEdge等等。

【问题讨论】:

  • 通过谷歌搜索“VBA selenium”可以获得大量信息 - 例如。 guru99.com/excel-vba-selenium.html
  • 到目前为止你是如何调整它的?你在哪里遇到了麻烦?请在您的问题中包含这一点。
  • 请不要在没有改进的情况下再次询问the same question。尝试专注于您遇到困难的特定问题,而不是广泛的编码问题。

标签: excel vba google-chrome microsoft-edge


【解决方案1】:

您需要使用 SeleniumBasic 在 VBA 中自动化 Edge。 SeleniumBasic 是一个基于 Selenium 的浏览器自动化框架,适用于 VB.Net、VBA 和 VBScript。

您可以按照以下步骤使用 SeleniumBasic 自动化 Edge 浏览器:

  1. this link 下载最新版本的 SeleniumBasic v2.0.9.0 并安装。
  2. this link下载对应版本的Edge WebDriver。
  3. 在我的情况下找到SeleniumBasic的路径C:\Users\%username%\AppData\Local\SeleniumBasic(也可能在此路径C:\Program Files\SeleniumBasic中),将Edge WebDriver msedgedriver.exe复制到此路径。
  4. msedgedriver.exe 重命名为edgedriver.exe
  5. 打开 Excel 并准备编写 VBA 代码。
  6. 在VBA代码界面,点击Tools > References,添加Selenium Type Library引用,点击OK保存。
  7. VBA代码示例(可根据需要更改):
    Public Sub Selenium()
        Dim bot As New WebDriver
        bot.Start "edge", "https://www.google.com"
        bot.Get "/"
        bot.Wait 5000
        bot.Quit
    End Sub
    

参考链接:VBA Script to convert from internet explorer to Edge or chrome browser

【讨论】:

  • 非常感谢于舟!这工作得很好!我试图使用驱动程序。而是机器人。正如我在我检查的所有其他参考资料中看到的那样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 2018-08-19
  • 1970-01-01
  • 2012-10-25
  • 2014-05-20
  • 1970-01-01
相关资源
最近更新 更多