Page 1 of 1

ComObjConnect/IE question

Posted: 27 Jul 2016, 13:32
by Miguel7
Hi guys,

So I've been trying to figure out a way to call AHK code when a user clicks a button or link in an IE window, *without* using an embedded AHK GUI. The reason I'm trying to avoid embedding IE in a GUI is it seems to break keyboard navigation (and accessibility is an important part of this project - and every project IMO but especially here). Pressing Tab seems to move the focus outside the embedded browser and into... something else. So that's not going to work here. So here's what I've got so far...

Code: Select all

; test.ahk - slightly modified from the AHK docs on ComObjConnect
ie := ComObjCreate("InternetExplorer.Application")

; Connects events to corresponding script functions with the prefix "IE_".
ComObjConnect(ie, "IE_")

ie.Visible := true  ; This is known to work incorrectly on IE7.
ie.Navigate(A_ScriptDir . "\test.html")
#Persistent

; Does nothing / event not fired / ???
; No message box, no errors, nuttin (again with the ??? lol)
IE_OnClick(document)
{
	window := document.parentWindow
	element:= window.event.srcElement
	MsgBox % element.id
}

; test.html - worked with an embedded GUI (another copy/paste off some forum post, but nothing too fancy here)
; EDIT: Note the "tabindex='0'" in the link - this was an attempt to fix the focus issue mentioned above when using an embedded GUI - didn't work)
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<body>
	<input type='text' id='Edit'/><br />
	<a href='#' id='Box' tabindex='0'>Show AHK message box</a>
</body>
</html>
Then when I restart or exit the script, I get any number of cryptic errors (ranging from "the interface is unknown" to "object invoked disconnected from blah blah deeblahdee blah" - lol). So I'm obviously missing something, gotta be something slight'n'stupid (as most bugs are) but so far it's code 1 me 0. lol idk - any clues would be greatly appreciated.

Re: ComObjConnect/IE question

Posted: 27 Jul 2016, 14:04
by Guest
https://autohotkey.com/boards/viewtopic.php?t=1267

Code: Select all

wb := ComObjCreate("InternetExplorer.Application")
wb.visible := True
wb.Navigate("http://ahkscript.org")
while wb.busy
	sleep 100
doc := wb.document
ComObjConnect(doc, "Doc_")
Gui, Show
Return
 
GuiClose:
ExitApp
 
Doc_OnClick(doc) {
    MsgBox, % doc.parentWindow.event.srcElement.OuterHtml
}

Re: ComObjConnect/IE question

Posted: 27 Jul 2016, 14:25
by Miguel7
Wow, thanks for the quick response on this! But how would it work with a local file? Does it require the full file:// address? Because for example if I try A_ScriptDir . "\test.html" it throws a tantrum. First it doesn't like "busy" and then it doesn't like "document" and then it says there's no valid COM object... any guesses? Thanks again.

EDIT: Tried with the full file:// address with no %variables% - still has kittens.

Re: ComObjConnect/IE question

Posted: 27 Jul 2016, 14:36
by Guest
That's strange.
The below code works for me when pointed local. Does the full file location work for you?

Code: Select all


;Gui Add, ActiveX, w500 h300 vwb, Shell.Explorer
wb := ComObjCreate("InternetExplorer.Application")
wb.visible := True
wb.Navigate("C:\Users\Administrator\Desktop\Google.htm")
while wb.busy
	sleep 100
doc := wb.document
ComObjConnect(doc, "Doc_")
Gui, Show
Return
 
GuiClose:
ExitApp
 
Doc_OnClick(doc) {
    MsgBox, % doc.parentWindow.event.srcElement.OuterHtml
}

Re: ComObjConnect/IE question

Posted: 27 Jul 2016, 14:49
by Miguel7
No dice. I had to change the file path/name, but apart from that I kept it exactly as you showed me. But it's still coughing up the same 3 errors:

First: it screams about "busy"...

---------------------------
test.ahk
---------------------------
Error: 0x80004005 - Unspecified error

Source: (null)
Description: (null)
HelpFile: (null)
HelpContext: 0

Specifically: busy

Line#
006: wb := ComObjCreate("InternetExplorer.Application")
007: wb.visible := True
008: wb.Navigate("C:\Users\mfox\Desktop\test.html")
---> 009: While,wb.busy
010: Sleep,100
011: doc := wb.document
012: ComObjConnect(doc, "Doc_")
013: Gui,Show
014: Return
017: ExitApp
019: {

Continue running the script?
---------------------------
Yes No
---------------------------

If I click "yes", it spits this:
---------------------------
test.ahk
---------------------------
Error: 0x80010108 - The object invoked has disconnected from its clients.


Specifically: document

Line#
006: wb := ComObjCreate("InternetExplorer.Application")
007: wb.visible := True
008: wb.Navigate("C:\Users\mfox\Desktop\test.html")
009: While,wb.busy
010: Sleep,100
---> 011: doc := wb.document
012: ComObjConnect(doc, "Doc_")
013: Gui,Show
014: Return
017: ExitApp
019: {
020: MsgBox,doc.parentWindow.event.srcElement.OuterHtml
021: }

Continue running the script?
---------------------------
Yes No
---------------------------

And if I hit Yes again, it pukes this:
---------------------------
test.ahk
---------------------------
Error: No valid COM object!

Line#
006: wb := ComObjCreate("InternetExplorer.Application")
007: wb.visible := True
008: wb.Navigate("C:\Users\mfox\Desktop\test.html")
009: While,wb.busy
010: Sleep,100
011: doc := wb.document
---> 012: ComObjConnect(doc, "Doc_")
013: Gui,Show
014: Return
017: ExitApp
019: {
020: MsgBox,doc.parentWindow.event.srcElement.OuterHtml
021: }
022: Exit

Continue running the script?
---------------------------
Yes No
---------------------------
If I hit Yes again, the dumb thing finally shuts up but doesn't work (which makes sense, since it just finished spewing a ton of errors, lol) but idk what the heck could possibly be bugging things up this bad.

But I have an idea of an alternate approach: if I were to go with the GUI, is there a way to prevent the focus from leaving the embedded browser (so Tab and Shift-Tab behave like they normally would in IE, instead of going into GUI-limbo)? Just wondering because most of the posts I've read nowadays seem to go the GUI route, and even I've been able to embed an HTML file in a GUI... so maybe there's some trick out there I'm just not aware of yet. :)

Re: ComObjConnect/IE question

Posted: 27 Jul 2016, 15:44
by Guest
I'm not sure why you are getting so many errors. I can run the script with the webpage embedded in a GUI or not embedded. I am running Win7 64bit on IE 11 with admin privileges.

Re: ComObjConnect/IE question

Posted: 20 Mar 2017, 16:45
by gongnl
bump,i have same problem,who help me?thank

Re: ComObjConnect/IE question

Posted: 20 Mar 2017, 17:15
by A_AhkUser
Invité wrote:I'm not sure why you are getting so many errors. I can run the script with the webpage embedded in a GUI or not embedded. I am running Win7 64bit on IE 11 with admin privileges.
for sure it's work.
just tried it out with a badly written html:
<
<head>

<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8" />
<title>HTMLFile</title>
</head>
<body>
<span class="X" style="display: none;"></span>
<div class="C" style="padding: 15px;">
</div>
</body>
</html>

and it give me the same error code as you miguel: 0x800004005

so it's probably your html code


EDIT:
miguel7 wrote: the full file://
the full is file:///

Re: ComObjConnect/IE question

Posted: 25 Mar 2017, 11:02
by Miguel7
Thanks, but I stopped using IE ages ago. Instead I've been using NW.js (a framework for creating desktop apps using HTML/JS/CSS). NW makes it possible to run AHK scrpts right from the DOM, and capture any input from stdout (i.e. if you type "FileAppend, someData, *" in AHK then run it from NW it will return "someData"). If you are still getting whatever error this was about, you might want to check it out.

Re: ComObjConnect/IE question

Posted: 19 Jan 2021, 16:46
by rhythmhighnoonstone
I've got the same error code `Error: 0x80004005` as well. It turned out that I didn't call `IE.Quit` in my previous script, so`IE := ComObjCreate("InternetExplorer.Application")` isn't available. I guess if you didn't close previous connection, you can't run another instance and hence, the error code.

For context here's my code (from the amazing jeeswg: https://www.autohotkey.com/boards/viewtopic.php?t=37345)

Code: Select all

a::
IE := ComObjCreate("InternetExplorer.Application")
IE.Navigate("www.google.com")
Loop
  if (IE.readyState = 4)	; wait for page to load
     Break
Question := IE.document.body.innerText
IE.Quit	; if you called this trigger without this line, calling this trigger again probably wouldn't work
Solution: restart your computer. For me, I use Process Explorer to kill all ieexplore.exe instances and my script works again.