看一下代码:
TStringList *__fastcall TFrame3::GetTablesRowsText2(CComPtr<IHTMLElement> ele, String strPhone1, String strPhone2)
{
TStringList *Result = new TStringList();
CComVariant rowIndex,rowvar2;
CComPtr <IHTMLElementCollection> rowcollection;
long p, q;
if ( ele ) {
CComPtr<IHTMLTable> table;
ele->QueryInterface(IID_IHTMLTable,(void**)&table);
table->get_rows(&rowcollection) ;
COM_CHECK(rowcollection-> get_length(&p));
IDispatch* pRowDisp;
for(int i=1;i <p;i++)
{//遍历所有元素
rowIndex = i;
HRESULT hr = rowcollection->item(rowIndex, rowvar2, &pRowDisp );
if ( hr == S_OK )
{
IHTMLTableRow * row;
hr = pRowDisp->QueryInterface(IID_IHTMLTableRow,(void **)&row);
if ( hr == S_OK )
{
IHTMLElementCollection * cellcollection;
hr = row->get_cells(&cellcollection);
LONG cellcount;
cellcollection->get_length(&cellcount);
for(int celli = 0; celli < cellcount; celli++)
{
VARIANT cellIndex;
cellIndex.vt = VT_UINT;
cellIndex.lVal = celli;
VARIANT cellvar2;
VariantInit( &cellvar2 );
IDispatch* pCellDisp;
HRESULT hr = cellcollection->item(cellIndex, cellvar2, &pCellDisp );
if ( hr == S_OK )
{
assert(pCellDisp);
IHTMLTableCell * cell;
hr = pCellDisp->QueryInterface(IID_IHTMLTableCell,(void **)&cell);
if ( hr == S_OK )
{
IHTMLElement * elename;
hr = cell->QueryInterface(IID_IHTMLElement,(void**)&elename);
if (hr == S_OK)
{
BSTR name;
elename->get_outerText(&name);
String tmpname(name);
if (celli == 0) {
if (tmpname.SubString(8,4) == strPhone1 || tmpname.SubString(8,4) == strPhone2 ) {
Result->Add(tmpname);
// ExeScript( "javascript:selphonetemp(" + IntToStr(rowIndex.intVal) + ")" );
}
}
}
cell->Release();
}
pCellDisp->Release();
}
}
}
}
}
pRowDisp->Release();
}
return Result;
}