;
MS Excel:
;
A test script:
;
Code:
COM_Init()
Excel_Open()
Columns = ABCDEFGH
Rows = 8
Loop, Parse, Columns
{
a := A_LoopField
Loop, %Rows%
Excel_SetText(a, A_Index, a . A_Index)
}
COM_Term()
;
a test script for getting value of a cell
;
Code:
COM_Init() ; Initialize COM
Excel_Open()
MsgBox Type some text in cell A1`nThen press #a
Return
#a::
MsgBox, % Excel_GetCell("A",1) ; <- change this
return
esc::
Excel_Close()
Sleep, 1000 ; sometimes i get rogue excel processes without this....why??
COM_Term()
exitapp
;#Include Excel.com.ahk
;#Include COM.ahk
;
;
Code:
example for inserting vbscript and executing it.
VBcode=
(
Sub Anything()
MsgBox("YEAH")
End Sub
)
COM_Init()
Excel_ImportCode(VBcode) ; you cant call this twice with the same code
Excel_Run("Anything")
COM_Term()
return
Excel_Run(sFunction){
if oExcel := COM_GetActiveObject("Excel.Application")
COM_Invoke(oExcel,"Run", sFunction)
COM_Release(oExcel)
}
Excel_ImportCode(VBcode){
if fileexist(A_ScriptDir . "\tempvbcode.txt")
FileDelete, %A_ScriptDir%\tempvbcode.txt
FileAppend, %VBcode%, %A_ScriptDir%\tempvbcode.txt
if oExcel := COM_GetActiveObject("Excel.Application")
if oActiveWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
if oVBProject := COM_Invoke(oActiveWorkbook,"VBProject")
if oVBComponents := COM_Invoke(oVBProject,"VBComponents")
COM_Invoke(oVBComponents,"Import", A_ScriptDir . "\tempvbcode.txt")
COM_Release(oVBComponents)
COM_Release(oVBProject)
COM_Release(oWorkbooks)
COM_Release(oExcel)
}
;
;
Code:
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 06/29/2008 21:35
; Function Name: Excel_GetCell()
;
; get text from the specified cell
;
; Syntax:
; Excel_GetCell(ColumnLetter, RowNumber)
;
; Example:
; MsgBox % Excel_GetCell("A", 1)
;
; Parameters:
; 1) ColumnLetter = The column letter
; 2) RowNumber = The row Number
; Return:
; Success = the value of the cell
; Failure = nothing
; **************************************************************************
Excel_GetCell(ColumnLetter, RowNumber){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
if !oCells := COM_Invoke(oExcel,"Cells",RowNumber,Excel_GetColumnIndex(ColumnLetter)) {
MsgBox Could not get cells.
Return
}
Value := COM_Invoke(oCells,"Value")
COM_Release(oCells)
COM_Release(oExcel)
return Value
}
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 05/19/2008 16:47
; Function Name: Excel_SetText()
;
; Syntax:
; Excel_SetText(ColumnLetter, RowNumber, Text)
; Parameters:
; 1) ColumnLetter = The column letter to put the text in
; 2) RowNumber = The row Number to put the text in
; 3) Text = The text to put in the cell
; Return:
; Success = nothing
; Failure = nothing
; **************************************************************************
Excel_SetText(ColumnLetter, RowNumber, Text){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
;Get the cell range
if !oCells := COM_Invoke(oExcel,"Cells",RowNumber,Excel_GetColumnIndex(ColumnLetter)) {
MsgBox Could not get cell range.
Return
}
COM_Invoke(oCells,"Formula=",Text)
COM_Release(oCells)
COM_Release(oExcel)
}
; get the index corresponding to the column
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 05/19/2008 16:55
; Function Name: Excel_GetColumnIndex()
;
; Syntax:
; Excel_GetColumnIndex(ColumnLetter)
; Parameters:
; 1) ColumnLetter =
; Return:
; Success =
; Failure =
; **************************************************************************
Excel_GetColumnIndex(ColumnLetter){
local t1 := 0, t2 = 0
StringUpper, ColumnLetter, ColumnLetter
Loop, Parse, ColumnLetter
t%A_Index% := Asc(A_LoopField) - 64
t2 != 0 ? Col := (26 * t1) + t2 : Col := t1
Return Col
}
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 05/19/2008 16:54
; Function Name: Excel_Open()
;
; Create a new excel instance
;
; Syntax:
; Excel_Open()
; Parameters:
; None
; Return:
; Success = nothing
; Failure = nothing
; **************************************************************************
Excel_Open(){
oExcel := COM_CreateObject("Excel.Application")
COM_Invoke(oExcel,"Visible=",True)
COM_Invoke(oExcel,"Activate")
oWorkbooks := COM_Invoke(oExcel,"Workbooks")
COM_Invoke(oWorkbooks,"Add")
COM_Release(oWorkbooks)
COM_Release(oExcel)
}
Excel_Close(){
oExcel := COM_GetActiveObject("Excel.Application")
COM_Invoke(oExcel,"Quit")
COM_Release(oExcel)
Sleep, 1000
}
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 05/19/2008 16:55
; Function Name: Excel_OpenDoc()
;
; Open the specified document....
;
; Syntax:
; Excel_OpenDoc(FileName)
; Parameters:
; 1) FileName = should be a FULL PATH
; Return:
; Success =
; Failure =
; **************************************************************************
Excel_OpenDoc(FileName){
oExcel := COM_CreateObject("Excel.Application")
COM_Invoke(oExcel,"Visible=",True)
COM_Invoke(oExcel,"Activate")
oWorkbooks := COM_Invoke(oExcel,"Workbooks")
COM_Invoke(oWorkbooks,"Open",FileName) ; open the file
COM_Release(oWorkbooks)
COM_Release(oExcel)
}
; **************************************************************************
; Author: NKRUZAN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 05/19/2008 16:55
; Function Name: Excel_GetActiveTitle()
;
;Get the title of the Active workbook open in Excel
;
; Syntax:
; Excel_GetActiveTitle()
; Parameters:
; None
; Return:
; Success =
; Failure =
; **************************************************************************
Excel_GetActiveTitle(){
oExcel := COM_CreateObject("Excel.Application")
COM_Invoke(oExcel,"Visible=",True)
COM_Invoke(oExcel,"Activate")
oActiveWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
Name := COM_Invoke(oActiveWorkbook,"Name") ; Get the name
COM_Release(oActiveWorkbook)
COM_Release(oExcel)
return Name
}
;
;
Code snippets from later posts in this thread
;;
Sean - Yet another (inconvenient) method to load an office document:
;http://www.autohotkey.com/forum/viewtop ... 771#197771;
Code:
COM_Init()
pxlb := COM_GetObject(A_Temp . "\test.xls") ; Excel Workbook
pxls := COM_Invoke(pxlb, "Worksheets", 1) ; Excel Worksheet
pxla := COM_Invoke(pxlb, "Application") ; Excel Application
pwns := COM_Invoke(pxla, "Windows")
pwin := COM_Invoke(pwns, "Item", COM_Invoke(pwns, "Count"))
COM_Invoke(pwin, "Visible=", True)
COM_Invoke(pxla, "Visible=", True)
Sleep 5000
COM_Invoke(pxlb, "Save")
COM_Invoke(pxla, "Quit")
COM_Term()
;
;
Embed/Edit in AHK's GUI an office document:
;http://www.autohotkey.com/forum/viewtop ... 771#197771;
Code:
GoSub, GuiOpen
pweb := COM_AtlAxCreateControl(WinExist(), A_Temp . "\test.xls")
pxlb := COM_Invoke(pweb, "Document") ; Excel Workbook
pxls := COM_Invoke(pxlb, "Worksheets", 1) ; Excel Worksheet
Return
GuiOpen:
Gui, +Resize +LastFound
Gui, Show, w800 h600 Center, Excel
COM_AtlAxWinInit()
Return
GuiClose:
Gui, Destroy
COM_Invoke(pxlb, "Save")
COM_Release(pxls)
COM_Release(pxlb)
COM_Release(pweb)
COM_AtlAxWinTerm()
ExitApp
;
;
Sean - Getting active excel instance
;http://www.autohotkey.com/forum/viewtop ... 761#259761;
Code:
COM_Init()
oExcel := GetExcel()
oWord := GetWord()
COM_Term()
Return
GetExcel(hWnd = 0)
{
DetectHiddenWindows, On
If Not hWnd
WinGet, hWnd, ID, ahk_class XLMAIN
ControlGet, hWnd, hWnd,, EXCEL71, ahk_id %hWnd%
If hWnd
Return GetNativeOM(hWnd)
}
GetWord(hWnd = 0)
{
DetectHiddenWindows, On
If Not hWnd
WinGet, hWnd, IDLast, ahk_class OpusApp
ControlGet, hWnd, hWnd,, _WwG1, ahk_id %hWnd%
If hWnd
Return GetNativeOM(hWnd)
}
GetNativeOM(hWnd)
{
Static pfn,iid
If Not pfn
pfn := DllCall("GetProcAddress", "Uint", DllCall("LoadLibrary", "str", "oleacc"), "str", "AccessibleObjectFromWindow"), COM_GUID4String(iid, "{00020400-0000-0000-C000-000000000046}")
If DllCall(pfn,"Uint",hWnd,"Uint",-16,"Uint",&iid,"UintP",pwin)=0
papp := COM_Invoke(pwin,"Application"), COM_Release(pwin)
Return papp
}
;
;
enigmatiqk - various functions
;http://www.autohotkey.com/forum/viewtop ... 285#260285;
Code:
; **************************************************************************
; Author: BENJAMIN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 02/04/2009 16:55
; Function Name: Excel_SetSheet(sheet) / Excel_GetSheet()
;
;Get or Set the working sheet used
;
; Syntax:
; Excel_SetSheet(sheet) / Excel_GetSheet()
; Parameters:
; thisSheet
; Return:
; Success =
; Failure =
; **************************************************************************
Excel_SetSheet(thisSheet){
global
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
sheet := thisSheet
oWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
ws := COM_Invoke(oWorkbook, "Worksheets",sheet)
COM_Invoke(ws, "activate")
}
Excel_GetSheet(){
global
return sheet
}
Excel_CheckSheet(thisSheet){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
test := thisSheet
oWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
if COM_Invoke(oWorkbook, "Worksheets",test)
return 1
else
return 0
}
; **************************************************************************
; Author: BENJAMIN
; Language: AutoHotkey v1.0.47.06
; Creation Date: 02/04/2009 16:55
; Function Name: Excel_AddSheet()
;
; Add a sheet in first position with name sheetX
;
; Syntax:
; Excel_AddSheet()
; Parameters:
; thisSheet
; Return:
; Success =
; Failure =
; **************************************************************************
Excel_AddSheet(thispos, name=""){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
oWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
oWorksheets := COM_Invoke(oWorkbook, "Worksheets")
pos := thispos
Excel_SetSheet(pos)
oWorksheet := COM_Invoke(oWorksheets,"Add")
ifNotEqual,name,
COM_Invoke(oWorksheet, "Name",name)
}
;position = B (before) or A(after)
Excel_MoveSheet(position,thisSheet,thisSheet2){
global
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
sheet = Sheets[%thisSheet%].Move
sheet2 := thisSheet2
oWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
ws2 := COM_Invoke(oWorkbook, "Sheets",sheet2)
ifEqual,position,B
COM_Invoke(oWorkbook, sheet,"+" ws2)
else
COM_Invoke(oWorkbook, sheet,"-0", "+" ws2)
}
Excel_RenameSheet(thisSheet,name){
global
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
sheet := thisSheet
oWorkbook := COM_Invoke(oExcel,"ActiveWorkbook")
ws := COM_Invoke(oWorkbook, "Worksheets",sheet)
COM_Invoke(ws, "Name",name)
}
;
;
enigmatiqk - Font styling
;http://www.autohotkey.com/forum/viewtop ... 500#260500;
Code:
; put param=1 set Style = true, param=2 set Style = false
Excel_StyleFont(ColumnLetter, RowNumber, Style,param){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
;Get the cell range
if !oCells := COM_Invoke(oExcel,"Cells",RowNumber,Excel_GetColumnIndex(ColumnLetter)) {
MsgBox Could not get cell range.
Return
}
oFont := COM_Invoke(oCells,"Font")
COM_Invoke(oFont,Style,param)
COM_Release(oFont)
COM_Release(oCells)
COM_Release(oExcel)
}
;
;
enigmatiqk - Cell styling
;http://www.autohotkey.com/forum/viewtop ... 559#260559;
Code:
Excel_StyleCell(ColumnLetter, RowNumber, Style,param){
if !oExcel := COM_GetActiveObject("Excel.Application") { ; Get the Already Running Instance
MsgBox Could not find Excel Instance.
Return
}
;Get the cell range
if !oCells := COM_Invoke(oExcel,"Cells",RowNumber,Excel_GetColumnIndex(ColumnLetter)) {
MsgBox Could not get cell range.
Return
}
IfInString,Style,Color
{
hex = 0x%param%
SetFormat, integer, d
hex -= 0
}
oInterior := COM_Invoke(oCells,"Interior")
COM_Invoke(oInterior,"Color",hex)
COM_Release(oInterior)
COM_Release(oCells)
COM_Release(oExcel)
}
;
;
Sean - SaveAs text
;http://www.autohotkey.com/forum/viewtop ... 373#263373;
Code:
COM_Invoke(oWorkbook, "SaveAs", "C:\1.txt", -4158) ; xlCurrentPlatformText
;