Graham
Graham

Reputation: 8151

Get the value of an input field in IE using AHK

I'm trying to get the value on an input field in IE using auto hot key.

I'm using AHK version 1.1.19.01 on windows 8.1 using IE 11.0.9600.17498

I have a simple (local) html page:

<!DOCTYPE html>
<html>
<head>
    <title>Page1</title>
</head>
<body>
    <input type="text" name="area2" id="area2" />
</body>
</html>

I type something in the text box and then run the ahk script (which should just tell me the value I typed in).

This is my ahk script:

wb := IEGet("Page1")
WinActivate Page1
txt := wb.Document.All.area2.Value
MsgBox % txt

IEGet(Name="")        ;Retrieve pointer to existing IE window/tab
{
    IfEqual, Name,, WinGetTitle, Name, ahk_class IEFrame
    {
        Name := ( Name="New Tab - Windows Internet Explorer" ) ? "about:Tabs" : RegExReplace( Name, " - (Windows|Microsoft) Internet Explorer" )
    }

    For wb in ComObjCreate( "Shell.Application" ).Windows
    {
        If ( wb.LocationName = Name ) && InStr( wb.FullName, "iexplore.exe" )
        {
            Return wb
        }
    }
}

The message box is blank.

I have tried various syntax to no avail. What am I doing wrong?

The IEGet function was copied from some web page - its not mine, but it works.

NOTES: To find the ahk version:

msgbox % "my ahk version: " A_AhkVersion

Upvotes: 1

Views: 2693

Answers (1)

blackholyman
blackholyman

Reputation: 1450

Here is a simple working example ( win7 v1.1.19.01 IE11 )

FileSelectFile, path

wb := ComObjCreate("InternetExplorer.Application")
wb.visible := true
wb.navigate(path)

while wb.readyState!=4 || wb.document.readyState != "complete" || wb.busy
    continue
return

f6::
msgbox % wb.document.all["area2"].value
return

I have sometimes also had problems with IEGet() and IE9+

but here is the function I use to get an active IE Object

WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}

It Query's the IWebBrowserApp interface and returns a usable IE Comobject

SetTitleMatchMode, 2

wb := WBGet("Page1")
txt := wb.Document.All["area2"].value
MsgBox % txt

Hope it helps

Upvotes: 1

Related Questions