guides:com:excel.application

Excel.Application

Original post by Learning one on the AutoHotkey Archived Forums


COM Object: Excel.Application

Purpose: Perform calculations, analyse information and visualise data in spreadsheets.

System Requirements: Microsoft Office Excel application

Documentation Link: Application Object (Excel)

Other Links: ColorIndex values

Basic Code Example:

oExcel := ComObjCreate("Excel.Application") ; create Excel Application object
oExcel.Workbooks.Add ; create a new workbook (oWorkbook := oExcel.Workbooks.Add)
 
oExcel.Range("A1").Value := 3 ; set cell A1 to 3
oExcel.Range("A2").Value := 7 ; set cell A2 to 7
oExcel.Range("A3").Formula := "=SUM(A1:A2)" ; set formula for cell A3 to SUM(A1:A2)
 
oExcel.Range("A1:A3").Interior.ColorIndex := 19 ; fill range of cells from A1 to A3 with color number 19
oExcel.Range("A3").Borders(8).LineStyle := 1 ; set top border line style for cell A3 (xlEdgeTop = 8, xlContinuous = 1)
oExcel.Range("A3").Borders(8).Weight := 2 ; set top border weight for cell A3 (xlThin = 2)
oExcel.Range("A3").Font.Bold := 1 ; set bold font for cell A3
 
A1 := oExcel.Range("A1").Value ; get value from cell A1, and store it in A1 variable
oExcel.Range("A4").Select ; select A4 cell
oExcel.Visible := 1 ; make Excel Application visible
MsgBox % A1 "`n" oExcel.Range("A2").Value ; check. You can use Round() function to round numbers to the nearest integer
ExitApp

How to access Workbook without opening it?

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?

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?

oExcel := oWorkbook.Application ; returns Excel application object that owns Workbook object