I have a CComPtr<IWebBrowser2> m_pWB2, how can I enumerate all the document elements?

Posted on 2009-07-02
Last Modified: 2013-12-04
In my DOWNLOADCOMPLETE event I want to enumerate all ATML tags, and, say, see their InnerHTML content.

How do I get this done starting with my IWebBrowser2 * ?
Question by:yossikally
  • 2
LVL 19

Expert Comment

ID: 24767025
The thread at provides a sample that might be exactly what you are looking for.

Looking at the description of the DownloadComplete event it states that it fires when a navigation operation finishes, is halted, or fails which might give you trouble when you try to traverse the document especially if the latter two cases occur. Perhaps NavigateComplete or NavigateComplete2 might be better events.
LVL 19

Accepted Solution

LordOfPorts earned 500 total points
ID: 24768614 is also an example based on the code referenced in the first post:
void YourCHtmlViewDerivedClass::OnNavigateComplete2(LPCTSTR strURL)


	CComPtr<IDispatch> spDisp = CHtmlView::GetHtmlDocument();

	IHTMLDocument2 * pHTML = NULL;

	IHTMLElementCollection * pElement = NULL;

	VARIANT vtName, vtIndex;

	BSTR bstrVal;

	CString sInnerHTML;

	spDisp->QueryInterface(IID_IHTMLDocument2, (LPVOID *) &pHTML);



	LONG nItems = 0;


	for(int i = 0; i < nItems; i++) {

		vtName.vt = VT_I4;

		vtName.lVal = i;

		vtIndex.lVal = 1;

		vtIndex.vt = VT_I4;

		CComPtr<IDispatch> spDispTemp;

		pElement->item(vtName, vtIndex, &spDispTemp);

		IHTMLElement * pItem = NULL;

		spDispTemp->QueryInterface(IID_IHTMLElement, (LPVOID*)&pItem);

		if(pItem == NULL) {

			AfxMessageBox(_T("pItem is NULL"));




		sInnerHTML = bstrVal;




Open in new window


Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now