2
votes

Can't we use a COM object created in a function outside the function?

strHTML := "<html><body>foobar</body></html>"
oNode := GetNode(strHTML, "body", 0)
msgbox % oNode.innerText

GetNode(strHTML, strNode, nIndex) {
    doc := ComObjCreate("HTMLfile") 
    doc.write(strHTML)
    return doc.getElementsByTagName(strNode)[nIndex]
}

Error: 0x80070005 - Access is denied.

Specifically: innerText

Line# 001: strHTML := "foobar" 002: oNode := GetNode(strHTML, "body", 0) ---> 003: MsgBox,oNode.innerText 005: { 006: doc := ComObjCreate("HTMLfile") 007: doc.write(strHTML) 008: Return,doc.getElementsByTagName(strNode)[nIndex] 009: } 010: Exit 011: Exit

Neither a global variable nor a byref parameter did help.


@SouthStExit This doesn't work so it becomes pointless to wrap it in a function. But as Lexikos mentioned, I see now the object gets destroyed so it becomes not accessible.

oNode1 := GetNode("<html><body>foo</body></html>", "body", 0)
oNode2 := GetNode("<html><body><div>bar</div></body></html>", "div", 0)

msgbox % oNode1.innerText "`n" oNode2.innerText

GetNode(strHTML, strNode, nIndex) {
    global doc
    doc := ComObjCreate("HTMLfile") 
    doc.write(strHTML)
    return doc.getElementsByTagName(strNode)[nIndex]
}
1

1 Answers

0
votes

You have to make doc global.

strHTML :=  "<html><body>foobar</body></html>"
oNode :=    GetNode(strHTML, "body", 0)
msgbox %    oNode.innerText
return

GetNode(strHTML, strNode, nIndex) {
    global  doc
    doc :=  ComObjCreate("HTMLfile"), doc.write(strHTML)
    return  doc.getElementsByTagName(strNode)[nIndex]
}