Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How do I set HTMLDocument to an existing Chrome page?

0

I can get the handle to the Chrome window but can't figure out how to get to the document.

This code opens a new (maybe incognito) window.

Set mHtml = New HTMLDocument

Set mReq = CreateObject("MSXML2.XMLHTTP")

mReq.Open "GET", Website, False

mReq.setRequestHeader "User-Agent", "Chrome"

mReq.send

mHtml.body.innerHTML = mReq.responseText

Answer
Discuss

Answers

0

Hi JFT and welcome to the Forum.

I'm not clear what you're thinking to do here. If I use your code extract but change "Website" to this Forum's URL  then print part of it (see changes in bold below) :

Sub ShowHTML()

Set mHtml = New HTMLDocument

Set mReq = CreateObject("MSXML2.XMLHTTP")
' Define a website
Website = "https://www.teachexcel.com/talk/microsoft-office"

mReq.Open "GET", Website, False
mReq.setRequestHeader "User-Agent", "Chrome"

mReq.send

mHtml.body.innerHTML = mReq.responseText

' extract part of document to Intermediate Window
Debug.Print Left(mHtml.body.innerHTML, 208)

End Sub

then the printed result is this (the first 208 characters only):

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="keywords" content="Excel Q&amp;A Forum">
<meta name="description" content="Microsoft Excel Question &amp; Answer Forum.">

To me that shows that (with the Microsoft HTML Object Library active) the HTML has been obtained (but will be much more than the initial 208 characters). I guess your task is to identify and extract which parts you want.

Hope this helps. If so, please don't forget to mark this Answer as Selected.

Discuss

Discussion

John_Ru,

Thank you for responding so quickly!

I left out the Website value because I had set it earlier in the code. I still believe that both codes are actually opening an incognito window that never is visible. I say this for two reasons. First, the page I am attempting to read is on a private network and requires a login. Secondly, when I look at the values in the local window, I see error messages and none of the elements that I can see when inspecting the page in the browser window.

What I am attempting to do is grab the page that I have already logged into and navigated to in an already open browser window. I can do this with Edge but it is cumbersome because I have to be in IE mode.

Sorry for the long explanation...

FWIW, I can use Chrome or Firefox.
JFT Apr 21, '23 at 1:32 pm
JFT

Can't do anything about your problem now but as far as I recall, you need the Selenium web driver to control Chrome. If you get that set up correctly, you can login and grab data (as you could with IE) assuming the target site doesn't use anti-scraping measures. Not really an area I'm that knowledgeable in, sorry.
John_Ru (rep: 5147) Apr 21, '23 at 2:21 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login