How can I get og:image
from resource with VBA
excel 2007
For example, this URL:
https://www.bbc.com/reel/video/p08jgfdg/the-truth-about-christopher-columbus
With a more recent version of Excel, you could try this:
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
But since your question is for Excel 2007, you would have to define the HttpRequest
like this instead:
'Required library reference: Microsoft XML v3.0 or v5.0
Dim HttpRequest As MSXML2.XMLHTTP
Set HttpRequest = New MSXML2.XMLHTTP
And if you wanted a function that would just return the URL as a string instead, you could easily edit the Sub procedure to make it a function taking MyUrl as a parameter and returning the string instead of using it to insert the image in the Activesheet (like this for example).