Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建VBA,返回Google和microsoft edge中搜索前5名的搜索结果_Vba_Google Chrome_Microsoft Edge - Fatal编程技术网

创建VBA,返回Google和microsoft edge中搜索前5名的搜索结果

创建VBA,返回Google和microsoft edge中搜索前5名的搜索结果,vba,google-chrome,microsoft-edge,Vba,Google Chrome,Microsoft Edge,我是VBA新手,但我想创建一个宏,使用google chrome或microsoft edge返回单元格A15中列出的项目的前5个搜索结果?我尝试添加下面的代码,但出现错误。VBA只能自动运行Internet Explorer浏览器。因此,如果你想使用谷歌浏览器或微软边缘,那么这种方法将不适用于你 下面是将Google搜索结果填充到工作表的示例代码 Option Explicit Public Sub GetLink() Dim ie As New InternetExplorer

我是VBA新手,但我想创建一个宏,使用google chrome或microsoft edge返回单元格A15中列出的项目的前5个搜索结果?我尝试添加下面的代码,但出现错误。

VBA只能自动运行Internet Explorer浏览器。因此,如果你想使用谷歌浏览器或微软边缘,那么这种方法将不适用于你

下面是将Google搜索结果填充到工作表的示例代码

Option Explicit
Public Sub GetLink()
    Dim ie As New InternetExplorer

    Dim url As String
    url = "https://google.co.uk/search?q=" + Sheet1.Range("A2").Value
    With ie
        .Visible = True
        .navigate url

        While .Busy Or .readyState < 4: DoEvents: Wend

        Sheet1.Range("B2").Value = .document.querySelector("#search div.r [href*=http]").href
        Sheet1.Range("C2").Value = .document.querySelector("#search div.r [href*=http]").innerText
        .Quit
    End With

End Sub
选项显式
公共子GetLink()
Dim ie成为新的InternetExplorer
将url设置为字符串
url=”https://google.co.uk/search?q=“+1.范围(“A2”).值
与ie
.Visible=True
.浏览网址
当.Busy或.readyState<4:DoEvents:Wend时
Sheet1.Range(“B2”).Value=.document.querySelector(“#search div.r[href*=http]”)。href
Sheet1.Range(“C2”).Value=.document.querySelector(“#search div.r[href*=http]”)。innerText
退出
以
端接头
输出:

参考:

下面是另一个有用的线程。它的解决方案使用XMLHTTP对象


此外,您可以尝试检查上述示例,并尝试根据自己的要求修改样本。

在完成研究后,您迄今为止尝试了什么?请在你的问题中包括这一点。另外,请解释一下你在使用它时遇到的问题。我用当前的问题更新了上面的问题。嗨-谢谢你的更新。我试着输入代码,结果出错了。我在上面的原始问题中删除了错误截图。错误是编译错误-未定义用户定义类型。请转到工具->参考。检查了“Microsoft Internet控件”和“Microsoft HTML对象库”的引用。然后,再次运行代码。请看这里: