How to Access the .Document Property to Parse Web Pages Using the New Edge Browser Control
Daniel Pineault is one of the best resources for information on the new Edge-based browser control:
You can find his articles on the web at DevHut.net and videos on his relatively new YouTube channel.
The Missing .Object
Property
One of the glaring omissions from the new Edge-based browser control is the .Object
property, which provided direct access to a VBA Object with properties and methods to navigate the Document Object Model.
Luckily for us, Daniel spent many hours to discover what was ultimately a fairly simple workaround for this lost functionality. He provides background information on the problem, along with his solutions and a few sample use cases in his article, Advanced Automation With The New Access Modern Web Browser Control:
"Now, with the previous web browser control, this wasn’t an issue. We could simply pass the Object.Document to an MSHTML.HTMLDocument object and then we could work with that directly to our hearts content. Something like:"
Dim oHTMLDoc As MSHTML.HTMLDocument
Set oHTMLDoc = Me.WebBrowser0.Object.Document
"But with the loss of the Object.Document, this is no longer an option for us to perform automation on the displayed webpage."
Daniel Pineault's Workaround
Daniel spent a few days searching for a workaround, eventually discovering a solution.
"In the end, I used 2 HTMLDocuments! One to write the content to, the other to actual automate.
"So, to reproduce what was previously possible, we simply do:"
Dim oHTMLFile As MSHTML.IHTMLDocument
Dim oHTMLDoc As MSHTML.HTMLDocument
Set oHTMLFile = New MSHTML.htmlDocument
oHTMLFile.Open
oHTMLFile.write ""
oHTMLFile.write Me.EdgeBrowser0.RetrieveJavascriptValue("document.getElementsByTagName('html')[0].outerHTML")
oHTMLFile.Close
Set oHTMLDoc = oHTMLFile
"Once we have the oHTMLDoc object, we can then perform all the automation/parsing/analysis we previous could."
Daniel goes on to provide some additional sample code at his site. Check it out and consider donating if you find the code useful:
Cover image generated with DALL-E-3