iterate over IHTMLElementCollection

4.3k Views Asked by At

is there a way to iterate over IHTMLElementCollection?

such as

var
  e : IHTMLLinkElement;
elementCollection:IHTMLElementCollection;
begin
    for e in elementCollection do
      showmessage(e.caption);
end;

i know there is a property named _newEnum, however it is not supported in delphi as much as i could understand.

update: apperently links are IHTMLElement and not IHTMLLinkElement

2

There are 2 best solutions below

0
On BEST ANSWER
for I := 0 to Pred(elementCollection.length) do
begin
  e := elementCollection.item(I, EmptyParam) as IHTMLElement;
  //...
end;
2
On

The code to use _newEnum looks like this. If you're sure you've only got link elements items in the collection, you could change the as IHTMLElement part (and the elem value type) of the inner loop to as IHTMLAnchorElement (IHTMLLinkElement appears to be something entirely different)

uses MSHTML, ActiveX;

var
  collection:IHTMLElementCollection;
  enum:IEnumVariant;
  v:OleVariant;
  u:IUnknown;
  element:IHTMLElement;
begin
  //...
  enum:=collection._newEnum as IEnumVariant;
  while enum.Next(1,v,u)=S_OK do
   begin
    elem:=u as IHTMLElement;
    //...
   end;