【问题标题】:How can I get og:image from resource with VBA excel如何使用 VBA excel 从资源中获取 og:image
【发布时间】:2020-12-12 11:49:13
【问题描述】:

如何使用VBA excel 2007 从资源中获取og:image

例如,这个网址:

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

【问题讨论】:

  • 请包括您的编码尝试

标签: excel vba excel-2007


【解决方案1】:

使用更新版本的 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)。

【讨论】:

  • @bpy - 我刚刚意识到你的问题有Excel-2007 标签。我添加了一些内容,使其适用于该版本的 Excel。
  • 它在HttpRequest.Send 上中断...无论如何...有没有可能成为一个函数,以便我可以从任何单元格获取MyUrl,例如:=GetImageFromHead(A3)
  • @bpy – 如果你告诉我运行时错误代码,也许我可以帮忙。但是,因为我没有Excel 2007,所以测试起来有点困难。关于函数形式,您当然可以使用MyUrl作为参数来做到这一点,但是您希望函数返回什么?字符串形式的图像 URL?
  • ...我了解 excel 2007 的不足,感谢您的所有努力。但是有没有办法在这里识别错误?至于第二个问题:是的!我希望函数将图像 URL 作为字符串返回。非常感谢,再次!
  • @bpy – 让我们将此讨论发送至chat
猜你喜欢
  • 2012-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
相关资源
最近更新 更多