HTMLFile

Original post by jethrow on the AutoHotkey Archived Forums.


Object Name: HTMLFile

Purpose: Represents an HTML document. Can be used to read, write, & interact with HTML.

Documentation Link 1)

Other Links: W3Schools - Document Object

Basic Code Example - this example extracts all the text & URLs from the links on the Google Search Page:

;// download the webpage source
URLDownloadToFile, http://www.google.com, Google_HTML
FileRead, html, Google_HTML
FileDelete, Google_HTML
 
;// write the Google Source to an HTMLfile
document := ComObjCreate("HTMLfile")
document.write(html)
 
;// loop through all the links
links := document.links
while (A_Index<=links.length, i:=A_Index-1)
   list .= i ") " links[i].innerText "`nURL: " links[i].href "`n`n"
 
;// some URLs have "about:" rather than the domain
StringReplace, list, list, about:, http://www.google.com, All
 
MsgBox, %list%

This next example shows how an HTMLfile object* can be used to create an HTML Control:

color := HtmlBgColor()
 
;// HTML to be added to a GUI:
html =
(
   <body style='background-color:%color%;overflow:auto'>
      <span id='id' style='color:black'>text color</span> example
   </body>
)
 
;// create a simple GUI
Gui, Add, Button, x6 y60 w55 h20, Red
Gui, Add, Button, x71 y60 w55 h20, Blue
Gui, Add, ActiveX, x0 y-5 w140 h50 vdocument, HTMLFile
document.write(html)
Gui, Show, x399 y246 w138 h86, HTML
return
 
GuiClose:
   Gui, Destroy
   ExitApp
ButtonRed:
ButtonBlue:
   document.all("id").style.color := SubStr(A_ThisLabel,7)
   return
 
HtmlBgColor() {
   Format := A_FormatInteger
   SetFormat, IntegerFast, Hex
   color := SubStr(DllCall("GetSysColor", "int",15),3)
   SetFormat, IntegerFast, %Format%
   return SubStr(color,5,2) SubStr(color,3,2) SubStr(color,1,2) ;// switch from BGR -> RGB
}

*Note - the HTMLfile object isn't required to accomplish this. You could simply write the HTML when adding the ActiveX Control:

Gui, Add, ActiveX, x0 y-5 w140 h50 vDocument, MSHTML:%html%

This post has an example of a completely HTML based GUI.

1)
Microsoft stopped hosting their documentation for this object in 2017. The last archive.org archive of the page can be found here. All of Microsoft's links have been modified to redirect to Mozilla's generic "Document" documentation.