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

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 * ?
Who is Participating?
LordOfPortsConnect With a Mentor Commented:
...here 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

The thread at http://www.experts-exchange.com/Programming/Languages/CPP/Q_20194736.html provides a sample that might be exactly what you are looking for.

Looking at the description of the DownloadComplete http://msdn.microsoft.com/en-us/library/aa768331%28VS.85%29.aspx 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.