COM Object Reference

Post your working scripts, libraries and tools
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

COM Object Reference

01 Oct 2013, 23:35

This is a rebuild of the original COM Object Reference thread, minus the tangential discussions. Please keep an eye out for any potential code improvements to keep this repository up to date with the latest AHK changes.

jethrow's original opening post:

Purpose: to create a basic reference for commonly used COM objects.

AutoHotkey now supports COM natively (thank you Lexikos, Sean, & fincs :) ). This doesn't eliminate the COM Standard Library, but it does add quite a bit of functionality to AutoHotkey. However, if users don't know or understand any COM objects, this Native COM support is less appealing/useful. Therefore, I am creating this thread as somewhat of a basic COM object reference. Forum members can add a COM object "profile", and I will link it to the original post. Questions are also welcome. Here is the suggested BBCode for the COM obj profile:

Code: [Select all] [Download] GeSHi © Codebox Plus

COM Object: 
Purpose:
System Requirements:
Documentation Link:
Other Links:
Basic Code Example:

NOTE - for the Documentation Link, please provide a link to the method/properties documentation.

See Also: CLSID Registry Scanner - view information about COM/ActiveX components installed on your computer.
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

01 Oct 2013, 23:38

Retrieve file path of the selected file

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

01 Oct 2013, 23:43

COM Object: Scripting.Dictionary
Purpose: Object that stores data key, item pairs.
System Requirements: General
Documentation Link: Dictionary Object
Other Links:
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



... what can Scripting.Dictionary do that the built-in objects can't do?

Great question - Case Sensitive Keys & Enumerating Keys in Order of Creation (natively):

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Related: How make Associative array keys case sensitive
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

01 Oct 2013, 23:45

COM Object: WinHttpRequest
Purpose: Provides simple HTTP client functionality, allowing much more control than UrlDownloadToFile.
System Requirements: General*
Documentation Link: http://msdn.microsoft.com/en-us/library/aa384106.aspx
Basic Code Example:

Code: [Select all] [Download] GeSHi © Codebox Plus

WebRequest :=	ComObjCreate("WinHttp.WinHttpRequest.5.1")
WebRequest.Open("GET", "http://www.autohotkey.net/~Lexikos/AutoHotkey_L/docs/AHKL_ChangeLog.htm")
WebRequest.Send()
RegExMatch(WebRequest.ResponseText, "(?<=<h2>).*?(?=</h2>)", ver)
MsgBox % ver
WebRequest := ""


* Note MSDN says Windows 2000 or later required, but I've seen some other cases where this wasn't accurate; maybe they say that since they really don't support older versions of Windows. Furthermore, AutoHotkey_L requires Windows 2000 or later.
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

01 Oct 2013, 23:49

COM Object: HTMLFile
Purpose: Represents an HTML document. Can be used to read, write, & interact with HTML.
System Requirements: General
Documentation Link: document object
Other Links: W3Schools - Document Object
Basic Code Example - this example extracts all the text & URLs from the links on the Google Search Page:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



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

Code: [Select all] [Download] GeSHi © Codebox Plus

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


This post has an example of a completely HTML based GUI.
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

01 Oct 2013, 23:56

GFL SDK is a library to load/save easily graphics formats.

It has the features :

Import about 100 graphic file formats
Export about 40 graphic file formats
Support of 10/12/16bits per component
Color mode convert
Lut transforms (brightness, contrast, ...)
Filters (blur, average, ...)
Tools (resize, de-interlace, ...)
Multi-page file creation
JPEG lossless transform
EXIF reading (with or without loading picture)
IPTC reading/writting (with or without loading picture)
And many may other things...

GFL SDK is provided as FREEWARE for private non-commercial or educational use (including non-profit organization)


COM Object: GflAx
Purpose: Library to load images and photos easily.
Documentation Link: http://www.xnview.com/en/gfl.html ; methods and properties are documented in the help file that comes with the installer.
System Requirements: GFLAx (ActiveX/ASP component) v2.80
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 08:56

COM Object: ScriptControl
Purpose: Dynamically execute VBScript or JScript
NOTE: Many VBScript & JScript scripts can be translated to AHK, rather than using the ScriptControl object. (see the last example)
System Requirements: 32bit AHK (see here for 64bit compatability)
Documentation Link: Using the ScriptControl
Code Examples:

The first example will execute some basic VBScript:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



The next example will execute some basic JScript:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



This next example will execute JScript, which will execute VBScript:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


NOTE: The following 3 scripts are equivalent:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 09:03

COM Object: Microsoft Outlook
Purpose: Read and Write Appointments from calendar
System Requirements: Microsoft Outlook: tested with 2010 and 2007
Documentation: http://support.microsoft.com/kb/220595
Other Links: following is a translation of this vb.net script.


Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 09:07

COM Object: Google Desktop
Purpose: manually feed items for google desktop to index
System Requirements: Google Desktop
Documentation Link: http://code.google.com/apis/desktop/docs/indexapi.html
Other Links: following is a translation of this python plugin: kongulo.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 09:53

COM Object: InternetExplorer.Application
Purpose: Explore Websites
System Requirements: General
Documentation Link: Reference for Developers
Other Links: NavConstants, CMD IDs, Basic Webpage Controls
Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Note - Internet Explorer uses the iWebBrowser2 interface
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 09:55

COM Object: Shell.Application
Purpose: Access Explorer & IE Windows/Tabs; Open & Manipulate Windows
System Requirements: General
Documentation Link: Shell Object
Other Links: ShellWindows Collection
Code Example:

The Windows Property returns the ShellWindows Collection, which is extremely helpful for accessing Explorer/IE Windows. This example Enumerates all the Explorer & IE Windows/Tabs:

Code: [Select all] [Download] GeSHi © Codebox Plus

SetFormat, IntegerFast, Hex ;// display HWND in Hex

for Item in ComObjCreate("Shell.Application").Windows ;// loop through the ShellWindows Collection
data .= "Name:`t" Item.LocationName "`n"
. "URL:`t" Item.LocationURL "`n"
. "Prog:`t" Item.Name "`n"
. "`t" Item.FullName "`n"
. "HWND:`t" Item.HWND "`n`n"

MsgBox %data%


Several of the methods/properties for the Shell.Application object are simple to do with AHK's commands - such as showing certain Dialog boxes or Minimizing all windows. However, here are a few examples for moving windows around:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 09:59

COM Object: MSXML2.DOMDocument.6.0
Purpose: XML parser
System Requirements: Windows XP SP3, Vista, 7
Documentation Link: MSDN
Other Links: A Beginner's Guide to the XML DOM
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


This renders xpath() obsolete, except for vanilla scripts or Win2000 scripts.
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:04

COM Object: SAPI.SpVoice
Purpose: Text-to-Speech
System Requirements: General
Documentation Link: SpVoice Interface
Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:08

COM Object: ImageMagickObject COM+ Object
Purpose: ImageMagick® is a software suite to create, edit, and compose bitmap images.
System Requirements: ImageMagickObject COM+ Object
Documentation Link:
Other Links: http://www.imagemagick.org/script/binary-releases.php#windows
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Convert image to text:

Code: [Select all] [Download] GeSHi © Codebox Plus

oI :=	ComObjCreate("ImageMagickObject.MagickImage.1") 
oI.convert("montage.jpg", "montage.txt")

Output:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Get information about a single pixel of an image:

Code: [Select all] [Download] GeSHi © Codebox Plus

; http://www.imagemagick.org/script/fx.php 
oI := ComObjCreate("ImageMagickObject.MagickImage.1")
r := oI.identify("-format", "%[fx]", "montage.jpg")
MsgBox % r


Notes:
I sometimes need to automatically stitch images together.
I used GflAx previously, but there had to calculate canvas size and positioning myself.
Now ImageMagick does the job for me.

The scope of ImageMagick is endless. Explore!
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:11

COM Object: Word.Application
Purpose: create and edit various documents
System Requirements: Microsoft Office Word application
Documentation Link: http://msdn.microsoft.com/en-us/library/bb148369
Other Links:
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:14

COM Object: Excel.Application
Purpose: Perform calculations, analyse information and visualise data in spreadsheets.
System Requirements: Microsoft Office Excel application
Documentation Link: http://msdn.microsoft.com/en-us/library/bb149067
Other Links:
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



How to access Workbook without opening it?

Code: [Select all] [Download] GeSHi © Codebox Plus

FilePath :=	"C:\Book1.xlsx" ; example path
oWorkbook := ComObjGet(FilePath) ; access Workbook object
MsgBox % oWorkbook.Sheets(1).Range("A1").Value ; get value from A1 cell in first sheet


How to access active Excel Application object?

Use oExcel := Excel_Get(), not oExcel := ComObjActive("Excel.Application"). More info here.

How to access active Workbook?

Code: [Select all] [Download] GeSHi © Codebox Plus

try
oWorkbook := Excel_Get().ActiveWorkbook ; try to access active Workbook object
catch
return ; case when Excel doesn't exist, or it exists but there is no active workbook. Just Return or Exit or ExitApp.

; if there is active workbook, code continues execution...
oWorkbook.ActiveSheet.Range("B2").Value := "B2" ; set value of B2 cell in active sheet to "B2"


How to access Excel object from Workbook object?

Code: [Select all] [Download] GeSHi © Codebox Plus

oExcel :=	oWorkbook.Application ; returns Excel application object that owns Workbook object
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:18

May need to edit content, I believe the While-loop can now be done as a For-loop

COM Object: PowerPoint.Application
Purpose: Create powerpoint presentations
System Requirements:Microsoft Office Powerpoint application, OS>Win2000
Documentation Link:MSDN article,WMI article
Other Links:Application Object
Basic Code Example:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:24

COM Object: XStandard.Zip
Purpose: Zip & Unzip with many features
System Requirements: 32-bit OS, XZip.dll
Documentation Link: http://www.xstandard.com/en/documentation/xzip/
Other Links:


Basic Code Example:

How to archive (or zip) multiple files

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\golf.jpg", "C:\Temp\images.zip")
objZip.Pack("C:\Temp\racing.gif", "C:\Temp\images.zip")


How to archive (or zip) multiple files with different compression levels

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip","" ,"" , 9)
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip","" ,"" , 1)


How to archive (or zip) multiple files with default path

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip", True)
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip", True)


How to archive (or zip) multiple files with a custom pat

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\reports.doc", "C:\Temp\archive.zip", True, "files/word")
objZip.Pack("C:\Temp\boat.jpg", "C:\Temp\archive.zip", True, "files/images")


How to archive (or zip) multiple files using wildcards

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Pack("C:\Temp\*.jpg", "C:\Temp\images.zip")


How to unzip files

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.UnPack("C:\Temp\images.zip", "C:\Temp\")


How to unzip files using wildcards

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.UnPack("C:\Temp\images.zip", "C:\Temp\", "*.jpg")


How to get a listing of files and folder in an archive

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objContents := objZip.Contents(A_ScriptDir . "\test.zip")._NewEnum
For objItem in objZip.Contents(A_ScriptDir . "\test.zip")
Msgbox % objItem.Path . objItem.Name . "`n"


How to remove a file from an archive

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Delete("headshots/smith.jpg", "C:\Temp\images.zip")


How to move a file in an archive

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Move("headshots/jones.jpg", "staff/jones.jpg", "C:\Temp\images.zip")


How to rename a file in an archive

Code: [Select all] [Download] GeSHi © Codebox Plus

objZip :=	ComObjCreate("XStandard.Zip")
objZip.Move("headshots/jones.jpg", "headshots/randy-jones.jpg", "C:\Temp\images.zip")
User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:26

COM Object: Shell.Application
Purpose: Retrieves file properties.
System Requirements: General
Documentation Link: GetDetailsOf, ParseName
Other Links: Retrieving Extended File Properties
Basic Code Example - this example demonstrates how to retrieve the properties of a specified file :

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

User avatar
sinkfaze
Posts: 413
Joined: 01 Oct 2013, 08:01

Re: COM Object Reference

02 Oct 2013, 10:31

The previous example is for extended properties. This is for general properties.

COM Object: winmgmts
Purpose: Retrieves file&folder properties.
System Requirements: General
Documentation Link: Enumerating Folders and Folder Properties, Enumerating Files and File Properties
Basic Code Example - this example demonstrates how to retrieve the properties of a specified file and folder:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


Return to “Scripts and Functions”

Who is online

Users browsing this forum: Delta Pythagorean and 20 guests