I want Autohotkey to monitor an Excel 2010 window for the Close Command, intercept and cancel the command, run a script, and then finally close the window.
I understand that OnClose and BeforeClose are existing commands in vba, but I canNOT use .xlsm (Macro-Enabled) worksheets and this is on at least 5 different workstations so Personal.xlsb isn't an option either.
I've found several references that vba is "easy" to translate to COM which I'm just learning how to use, but I've been beating my head against the keyboard for a week now and I can come up with no syntax that will let me monitor a window for the "Close" command.
I was thinking most recently, using OnMessage to monitor for the WM_CLOSE, but I cannot come up with the syntax for that either and Google has been useless, I can't even find anyone else asking about this! Please Help!
Using AHK to Stop a Window from closing
-
- Posts: 47
- Joined: 09 Oct 2014, 11:44
- Blackholyman
- Posts: 1293
- Joined: 29 Sep 2013, 22:57
- Location: Denmark
- Contact:
Re: Using AHK to Stop a Window from closing
try something like this
more
Code: Select all
oExcel := ComObjActive("Excel.Application")
oWorkbook := oExcel.ActiveWorkbook
ComObjConnect(oWorkbook, Workbook_Events)
return
class Workbook_Events
{
BeforeClose(Cancel)
{
msgbox % "closing"
}
}
Code: Select all
oExcel := ComObjActive("Excel.Application")
oWorkbook := oExcel.ActiveWorkbook
ComObjConnect(oWorkbook, Workbook_Events)
return
class Workbook_Events
{
BeforeClose(Cancel)
{
; 0 == FALSE, -1 == TRUE
; typedef short VARIANT_BOOL
;You can store a "true" value like so:
NumPut(-1, ComObjValue(Cancel), "short")
msgbox lets count to 100
Loop 100
{
ToolTip %A_index%
sleep 10
}
ToolTip
; Used settimer to start a new thread
SetTimer, CloseExcel, -100
}
}
CloseExcel()
{
global oExcel, oWorkbook
ComObjConnect(oWorkbook)
oExcel.Quit
}
Courses on AutoHotkey
My Autohotkey Blog
![Yay! Dance! Yay! :dance:](./images/smilies/dance.gif)
-
- Posts: 47
- Joined: 09 Oct 2014, 11:44
Re: Using AHK to Stop a Window from closing
Blackholyman wrote:try something like this
moreCode: Select all
oExcel := ComObjActive("Excel.Application") oWorkbook := oExcel.ActiveWorkbook ComObjConnect(oWorkbook, Workbook_Events) return class Workbook_Events { BeforeClose(Cancel) { msgbox % "closing" } }
Code: Select all
oExcel := ComObjActive("Excel.Application") oWorkbook := oExcel.ActiveWorkbook ComObjConnect(oWorkbook, Workbook_Events) return class Workbook_Events { BeforeClose(Cancel) { ; 0 == FALSE, -1 == TRUE ; typedef short VARIANT_BOOL ;You can store a "true" value like so: NumPut(-1, ComObjValue(Cancel), "short") msgbox lets count to 100 Loop 100 { ToolTip %A_index% sleep 10 } ToolTip ; Used settimer to start a new thread SetTimer, CloseExcel, -100 } } CloseExcel() { global oExcel, oWorkbook ComObjConnect(oWorkbook) oExcel.Quit }
Thank you so much...never did get to test this, needs changed on me at the last minute.
Sorry to be so long in posting my gratitude, I was just going through my old forum posts and I realized I never responded
![Thumbdown :thumbdown:](./images/smilies/icon_thumbdown.gif)
Who is online
Users browsing this forum: bobstoner289, Google [Bot], peter_ahk, Spawnova and 341 guests