ihtmltable

In C++Builder, there is a useful component TCppWebBrowser, which could allow us to access internet web pages easily. While to manipulate the table elements is not so easy. The following are some codes doing this.

TCppWebBrowser* pWebBrowser;
IHTMLDocument2* pHtmlDoc2;
ASSERT(lpDispatch);

//Query IHTMLDocument2 interface
HRESULT hr = pWebBrowser->QueryInterface(IID_IHTMLDocument2, (void**)&pHtmlDoc2);
if(hr != S_OK)
{
    return FALSE;
}

//Getting table dispatch
IHTMLElementCollection *all;
pHtmlDoc2->get_all(&all);
IDispatch* distable;
all->item(COleVariant(strTableName.c_str()), COleVariant(short(0)), &distable);
IHTMLTable *pITable = NULL;
if(distable != NULL)
{
    hr = distable->QueryInterface(IID_IHTMLTable, (void**)&pITable);
    ASSERT(hr == S_OK);
}

//Getting row dispatch
IHTMLElementCollection *pIRows;
hr = pITable->get_rows(&pIRows);
ASSERT(hr == S_OK);
IDispatch *disrow;
hr = pIRows->item(COleVariant(long(nRow)),COleVariant(short(0)),&disrow);
if (hr != S_OK || disrow == NULL)
    return FALSE;

IHTMLTableRow *pIRow = NULL;
if(disrow != NULL)
{
    hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
    ASSERT(hr==S_OK);
}

//Getting cells
IHTMLElementCollection* rowcells = NULL;
if(pIRow != NULL)
{
    pIRow->get_cells(&rowcells);
}

IDispatch* discell = NULL;
if(rowcells != NULL)
{
    hr=rowcells->item(COleVariant(long(nColumn)),COleVariant(short(0)),&discell);//iCol col
    if(hr != S_OK || discell == NULL)
        return FALSE;
}

IHTMLElement* cell = NULL;
if(discell != NULL)
{
    hr = discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
    ASSERT(hr == S_OK);
}

//Getting cell values
CString strHtmlValue;
strHtmlValue = strValue.c_str();
BSTR bsStr = strHtmlValue.AllocSysString();
cell->put_innerHTML(bsStr);
SysFreeString(bsStr);

//Rerease interface
if (cell)
    cell->Release();

if (pIRow)
    pIRow->Release();

if (pITable)
    pITable->Release();