In my previous post:
I demonstrated how easy it was to gather any information from a site using basic Internet Explorer (IE) automation.
Sadly, IE is going to be killed off June 15th, refer to my post on the subject if you need more information on the subject

So my post regarding IE automation will only prove useful for a couple months and then cease to work.
Short sighted as Microsoft is, they did not enable any form of automation via VBA for Edge, so that’s also out of the question!
Where does that leave us exactly?
Well, luckily we still have our WebBrowser control. Although outdated and may not work with all sites 100%, it still offers us the ability to work with sites in a VERY similar manner as we previously did with IE automation.
What I did was create a simple form and insert a WebBrowser control and used the form’s open event to set its Control Source to the site I wanted to pull information from.
Private Sub Form_Open(Cancel As Integer)
Me.WebBrowser1.ControlSource = "=(""https://www.devhut.net/avoiding-the-followhyperlink-security-warning/"")"
End Sub
Then I simply added a command button, named: Cmd_Scrape, that when pressed get the information I’m after (it could be anything at all).
Here’s how I coded it:
Private Sub Cmd_Scrape_Click()
Dim oWebBrowser As Access.WebBrowserControl
Dim oWebBrowserObject As Object
#Const MSHTML_EarlyBind = False
#If MSHTML_EarlyBind = True Then
Dim oHTMLDoc As MSHTML.HTMLDocument 'Req Ref to Microsoft HTML Object Library
#Else
Dim oHTMLDoc As Object
#End If
On Error GoTo Error_Handler
Set oWebBrowser = Me.WebBrowser1
Set oWebBrowserObject = oWebBrowser.Object
Set oHTMLDoc = oWebBrowserObject.Document
Debug.Print "URL", oWebBrowser.LocationURL
Debug.Print "Title", HTMLDoc_GetTitle(oHTMLDoc) 'can also simply use oIE.LocationName
Debug.Print "Description", HTMLDoc_GetMetaContent(oHTMLDoc, "Description")
Debug.Print "Keywords", HTMLDoc_GetMetaContent(oHTMLDoc, "Keywords")
Debug.Print "Robots", HTMLDoc_GetMetaContent(oHTMLDoc, "Robots")
Debug.Print "Generator", HTMLDoc_GetMetaContent(oHTMLDoc, "Generator")
Debug.Print "Test", HTMLDoc_GetMetaContent(oHTMLDoc, "Test")
Error_Handler_Exit:
On Error Resume Next
If Not oHTMLDoc Is Nothing Then Set oHTMLDoc = Nothing
If Not oWebBrowserObject Is Nothing Then Set oWebBrowserObject = Nothing
If Not oWebBrowser Is Nothing Then Set oWebBrowser = Nothing
Exit Sub
Error_Handler:
MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: Cmd_Scrape_Click" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occurred!"
Resume Error_Handler_Exit
End Sub
and the code uses the following Helper Functions:
Private Function HTMLDoc_GetTitle(ByVal oHTMLDoc As Object) As String
Dim TitleElements As Object
Set TitleElements = oHTMLDoc.getElementsByTagName("title")
If TitleElements.length > 0 Then
HTMLDoc_GetTitle = TitleElements(0).innerHTML
End If
If Not TitleElements Is Nothing Then Set TitleElements = Nothing
End Function
Private Function HTMLDoc_GetMetaContent(ByVal oHTMLDoc As Object, ByVal sMetaName As String) As String
Dim MetaElements As Object
Dim MetaElement As Object
HTMLDoc_GetMetaContent = "***META NOT FOUND***"
Set MetaElements = oHTMLDoc.getElementsByTagName("meta")
For Each MetaElement In MetaElements
If LCase(MetaElement.Name) = LCase(sMetaName) Then
HTMLDoc_GetMetaContent = MetaElement.content
Exit For
End If
Next
If Not MetaElement Is Nothing Then Set MetaElement = Nothing
If Not MetaElements Is Nothing Then Set MetaElements = Nothing
End Function
As you can see, the code is nearly identical to IE automation. It’s just a question of a minor adjustment in defining the oHTMLDoc object that needs to be made, beyond that existing code should work fine.
All that to say that it is possible to salvage many existing solutions and adapt them to work with the WebBrowser control instead of IE directly. So all is not lost with IE being killed off.
Is there any way to use WebBrowser in Excel VBA as a one-for-one replacement for InternetExplorer? I would prefer not to create a userform, because it’s not needed for my application. I am using this to accomplish step 1 of the OAuth 2.0 authorization code grant flow, which involves navigating to a URL that redirects you to another URL which includes a code.
Previously, I was able to use IE to do this, setting visible = false to be transparent to the user. Whenever I try to use WebBrowser the same way, the Navigate method fails and I don’t know why. Is it literally required to be on a form to work? All I need to do is navigate to a URL that takes me to another URL, then extract part of the new URL.
Thanks in advance!
The Microsoft Web Browser control is disabled in Excel as of Office 2013. You can however re-enable it with a registry hack. You have to set the ‘Compatibility Flags’ = 0 for the ActiveX control ClassId, which is {8856F961-340A-11D0-A96B-00C04FD705A2} for the Microsoft Web Browser.
To answer your question. Just set the Height and Width = 0, then work code will work fine, but it won’t be visible to the user.
How do I get the Cookie header from the opened page in WebBrowser control on my current form? The Me.WebBrowser0.Object.Document.Cookie is empty but I know that the cookies are there!
Not something I’ve ever needed to do, but I’d probably simply use JavaScript to get the information. One way or another, this is the only possible approach with the new Web Browser control.
Sorry, can you provide a simple example? I have an old WebBrowser control on a form. How do I call Javascript on that page and return a value back to the VBA code? Thanks!
It depends what you’re trying to do exactly, but at its core you can simply run something like:
document.cookie.split(‘;’).join(‘;\r\n’);
As for how to run JS in the web browser control, I’ll refer you to my main article on the subject at: https://www.devhut.net/everything-you-never-wanted-to-know-about-the-access-webbrowser-control/#Code