Does someone have a piece of code that can show me how to collect all the links in a web browser document?
IHTMLDocument2 has links Property that should return a reference to IHTMLElementCollection but than ...?
Thanks a lot
Eros
Then parse the IHTMLElementCollection calling the length property to retrieve the number of items in the collection and the item method to retrieve individual items. Something like:
DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM pDispatch AS IDispatch
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM i AS LONG
DIM nCount AS LONG
DIM vIndex AS VARIANT
DIM vItem AS VARIANT
DIM strRef AS STRING
pIHTMLElementCollection = pIHTMLDocument2.links
nCount = pIHTMLElementCollection.length
FOR i = 0 TO nCount - 1
vIdx = i AS LONG
pDispatch = pIHTMLElementCollection.Item(vIdx)
pIHTMLLinkElement = pDispatch
pDispatch = NOTHING
IF ISNOTHING(pIHTMLLinkElement) THEN EXIT FOR
' Retrieve the properties
strRef = pIHTMLLinkElement.href
STDOUT "href = " & ACODE$(strRef)
...
...
' Release the object
pIHTMLLinkElement = NOTHING
NEXT
or enumerate it using the standard IEnumVARIANT interface:
DIM pIHTMLElementCollection AS IHTMLElementCollection
DIM hr AS LONG
DIM penum AS IEnumVARIANT
DIM pIHTMLLinkElement AS IHTMLLinkElement
DIM vItem AS VARIANT
DIM strRef AS STRING
pIHTMLElementCollection = pIHTMLDocument2.links
penum = pIHTMLElementCollection.newEnum_
DO
' Retrieve a reference to the next object in the collection
hr = pEnum.Next(1, vItem, BYVAL %NULL)
IF hr <> %S_OK THEN EXIT DO
' Assign the VT_DISPATCH variant to the object variable
pIHTMLLinkElement = vItem
vItem = EMPTY
IF ISNOTHING(pIHTMLLinkElement) THEN EXIT DO
' Retrieve the properties
strRef = pIHTMLLinkElement.href
STDOUT "href = " & ACODE$(strRef)
...
...
' Release the object
pIHTMLLinkElement = NOTHING
LOOP
Thank you José.
There must be something not working in first method (second not yet tested).
I correctly get the number of items in "count"
nCount = pIHTMLElementCollection.length
but than
If IsNothing(pIHTMLLinkElement) Then Exit For
never pass because "ISNOTHING(pIHTMLLinkElement)" is always true preventing the collection of hRef
Also with second method, IEnumVARIANT interface, the following is immediately TRUE and exit the DO loop
If IsNothing(pIHTMLLinkElement) Then Exit Do
See this thread on the PowerBASCI forum
http://www.powerbasic.com/support/pbforums/showthread.php?t=39275
and this thread for context.
http://www.powerbasic.com/support/pbforums/showthread.php?t=39078
Got it working.
Many thanks