I am by no means an expert at COM or Excel but I do my best.
Q: What is COM?
The Component Object Model is a collection of automation objects that allows a user to interface with various methods and properties of an application.
Q: How do I use it in my script?
There is no easy answer to this question. Why? Because there are different commands to every type of COM object. For instance the methods for Internet Explorer are completely different from MS Office.
In this tutorial I will focus on using COM to script simple commands that will be used to automate Microsoft Office Excel. Before you can do anything with the Excel DOM you have to create a handle to the application. There are 2 ways to do this:
Xl := ComObjCreate("Excel.Application") ;create a handle to a new excel application Xl := ComObjActive("Excel.Application") ;creates a handle to your currently active excel sheetUse one or the other depending on what kind of script you wish to put together. For example if I'm testing new excel code I like to use the active COM object so I don't have to wait on a new instance to open each time.
Let's look at each type individually:
ComObjCreate:
Xl := ComObjCreate("Excel.Application") ;handle Xl.Visible := True ;by default excel sheets are invisible Xl.Workbooks.Add ;add a new workbookSimple by accessing the object does not give us anything to work with or see. We need to create a workbook and make it visible to do anything. (don't ask me why Microsoft made the default invisible...)
You can also have the user choose a document to open.
FileSelectFile, Path Xl := ComObjCreate("Excel.Application") Xl.Workbooks.Open(Path) ;open an existing file Xl.Visible := TrueComObjActive:
Xl := ComObjCreate("Excel.Application") ;handleIt's fairly simple to set up an active sheet since it's already visible and the workbook is already open.
So far we have only retrieved a handle to a new sheet, a user specified sheet, or an active sheet. Now we can manipulate some data!
The most basic and useful way to use COM is to set individual cells in the sheet.
Xl.Range("A1").Value := "hello world!" ;set cell 'A1' to a string helloworld := "hello world!" Xl.Range("A1").Value := helloworld ;set cell to a variableYou can also set a variable to data inside a cell:
helloworld := Xl.Range("A1").ValueIt's not too useful to set specific cells in a script especially in large files. Here's how to loop through the cells in a column:
while (Xl.Range("A" . A_Index).Value != "") { Xl.Range("A" . A_Index).Value := value }And how to loop through a row:
Row := "1" Columns := Object(1,"A",2,"B",3,"C",4,"D",5,"E",6,"F",7,"G",8,"H",9,"I",10,"J",11,"K",12,"L",13,"M",14,"N",15,"O",16,"P",17,"Q") ;array of column letters For Key, Value In Columns XL.Range(Value . Row).Value := value ;set values of each cell in a rowI'm not going to go into how object work. That's for another time.
However in this example i'm using an object to store the column letters so i can loop through them. The 'key' is the position in the object the string is located. 'value' is what is stored in that position. So columns[1] = "A", columns[2] = "B", ect.
Now that you can use the basic's of excel let's look at a few more specific
Examples:
Xl.Range("A:A").Copy ;copy cell to clipboard
Xl.Range("A:A").PasteSpecial(-4163) ;'-4163' is the constant for values only
Xl.Range("A:A").NumberFormat := "@" ;change the column format to 'text'
Xl.CutCopyMode := False ;deselect cells (use this with copy)2 Save Methods:
XL.ActiveWorkbook.SaveAs(BookName) ;'bookname' is a variable with the path and name of the file you desire Xl_Workbook := Xl.Workbooks.Open(Path) ;handle to specific workbook Xl_Workbook.Save() ;quick save already existing fileSort sheet by column:
Xl.Range("A1:Q100").Sort(Xl.Columns(1), 1) ;sort sheet by data in the 'a' columnThat about completes this simple tutorial.
If you are interested in learning how to do other things with Excel the easiest way is to use the Macro button in the View tab on any 2007 Excel sheet. This handy feature records your actions within excel and then see the VBA (Visual Basic for Applications) code which is fairly easy to translate to autohotkey. Or you can post it in this topic for help.
Special Thanks:
a4u
Sinkfaze
jethrow
For helping a hard headed ninCOMpoop with some difficult code. :wink: