Using AHK to Stop a Window from closing

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
canisdibellum
Posts: 47
Joined: 09 Oct 2014, 11:44

Using AHK to Stop a Window from closing

29 Oct 2015, 03:28

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!
User avatar
Blackholyman
Posts: 1293
Joined: 29 Sep 2013, 22:57
Location: Denmark
Contact:

Re: Using AHK to Stop a Window from closing

29 Oct 2015, 04:01

try something like this

Code: Select all

oExcel := ComObjActive("Excel.Application")
oWorkbook := oExcel.ActiveWorkbook
ComObjConnect(oWorkbook, Workbook_Events)
return

class Workbook_Events 
{
	BeforeClose(Cancel)
	{
		msgbox % "closing"
	}
}
more

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
}
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
canisdibellum
Posts: 47
Joined: 09 Oct 2014, 11:44

Re: Using AHK to Stop a Window from closing

23 Jun 2016, 11:04

Blackholyman wrote:try something like this

Code: Select all

oExcel := ComObjActive("Excel.Application")
oWorkbook := oExcel.ActiveWorkbook
ComObjConnect(oWorkbook, Workbook_Events)
return

class Workbook_Events 
{
	BeforeClose(Cancel)
	{
		msgbox % "closing"
	}
}
more

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: Thanks again for your help

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: bobstoner289, Google [Bot], peter_ahk, Spawnova and 341 guests