excel - 我怎样才能得到 og :image from resource with VBA excel

标签 excel vba excel-2007

我怎样才能得到og:image来自 VBA 的资源excel 2007例如,这个网址:

https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus

最佳答案

使用更新版本的 Excel,您可以尝试以下操作:

Sub GetImageFromHead()

    Dim MyUrl As String
    MyUrl = "https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus"

    'Required library reference: Microsoft XML v6.0
    Dim HttpRequest As MSXML2.XMLHTTP60
    Set HttpRequest = New MSXML2.XMLHTTP60
    HttpRequest.Open "GET", MyUrl, False
    HttpRequest.Send
    
    Dim HtmlDoc As Object
    Set HtmlDoc = CreateObject("htmlfile")
    HtmlDoc.Write HttpRequest.responseText

    'This next line makes sure that the JavaScript on the page gets processed before continuing execution of the code.
    DoEvents 

    'Required library reference: Microsoft HTML Object
    Dim MetaCollection As MSHTML.IHTMLElementCollection
    Set MetaCollection = HtmlDoc.getElementsByTagName("meta")
    
    Dim HtmlElement As MSHTML.IHTMLElement
    For Each HtmlElement In MetaCollection
        If HtmlElement.getAttribute("property") = "og:image" Then
            ActiveSheet.Pictures.Insert (HtmlElement.getAttribute("content"))
        End If
    Next

End Sub
但由于您的问题是针对 Excel 2007,您必须定义 HttpRequest像这样:
    'Required library reference: Microsoft XML v3.0 or v5.0
    Dim HttpRequest As MSXML2.XMLHTTP
    Set HttpRequest = New MSXML2.XMLHTTP
如果您想要一个仅将 URL 作为字符串返回的函数,您可以轻松地编辑 Sub 过程,使其成为一个以 MyUrl 作为参数并返回字符串的函数,而不是使用它在 Activesheet 中插入图像( like this for example)。

关于excel - 我怎样才能得到 og :image from resource with VBA excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63550852/

相关文章:

excel - 如何克服 Excel 中公式的最大长度限制? excel的错误?

excel - 具有多个 OR/AND 标准的 SUMIFS(使用单元格引用)

excel - UDF 从通用字符串中提取特定数据

vba - Access 2013/2016不支持treeview控件,报错 "User defined type not defined"

excel - 在当前目录中打开工作簿

vba - 我怎样才能将它添加到数组中?

excel-2007 - 使用 OpenXML sdk 为 Excel 定义的格式样式列表在哪里

mysql - SQL查询,如何从一个列表中获取所有元素,而仅从另一个表中获取相似元素

excel - VBA:如何等到excel下载完成?

Excel 匹配多个条件