Accessing Word COM events from within a function Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Accessing Word COM events from within a function

27 Mar 2019, 08:27

Good morning. Yesterday I was finally able to figure out how to access COM events connected to a word document. However, when I wrap the code into a function, the events don't seem to trigger any more. I'd like to be able to make the first codeblock work correctly and create an event listener from within a function. I've also tried using the class in the COM basics thread, it fails to run from within a function as well.

Code: Select all

;Although this opens microsoft word, when you create a new document or close the document, the events are not triggered.
createWordListener()


createWordListener() {	
	wdApp := ComObjCreate("Word.Application") ;create a word app
	wdApp.Visible := true
	ComObjConnect(wdApp,"wd_") ;connect to events
	return
}

wd_DocumentBeforeClose(Doc, App) { ;This should trigger just before the document closes.
	MsgBox,  App Closing
}


wd_NewDocument(Doc,App) { ;when creating a new document, this triggers
	MsgBox, New Document
}
The following code, however, works perfectly

Code: Select all

	wdApp := ComObjCreate("Word.Application") ;create a word app
	wdApp.Visible := true
	ComObjConnect(wdApp,"wd_") ;connect to events
	return


wd_DocumentBeforeClose(Doc, App) { ;This should trigger just before the document closes.
	MsgBox,  App Closing
}


wd_NewDocument(Doc,App) { ;when creating a new document, this triggers
	MsgBox, New Document
}

Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: Accessing Word COM events from within a function

27 Mar 2019, 08:27

Thank you, in advance, for any assistance that can be offered!
awel20
Posts: 211
Joined: 19 Mar 2018, 14:09

Re: Accessing Word COM events from within a function  Topic is solved

27 Mar 2019, 09:10

In your function, the variable '"wdApp" is cleared as soon as the function returns. You need to save that reference somewhere. You could return it from the function. Or if you are only going to have one instance of word open then you can just make wdApp static so that it will keep its contents after the function returns. If you are possibly going to have more than one instance of word then you will need to save each individual reference somewhere, for example an array.
Stavencross
Posts: 82
Joined: 24 May 2016, 16:42

Re: Accessing Word COM events from within a function

27 Mar 2019, 10:18

awel20 wrote:
27 Mar 2019, 09:10
In your function, the variable '"wdApp" is cleared as soon as the function returns. You need to save that reference somewhere. You could return it from the function. Or if you are only going to have one instance of word open then you can just make wdApp static so that it will keep its contents after the function returns. If you are possibly going to have more than one instance of word then you will need to save each individual reference somewhere, for example an array.
Exactly as you said, this works perfectly. Thank you!

Code: Select all

#Persistent

 wdApp := runIt()


runIt() {	
	wdApp := ComObjCreate("Word.Application") ;create a word app
	wdApp.Visible := true
	 ComObjConnect(wdApp,"wd_") ;connect to events
	return wdApp
}

wd_DocumentBeforeClose(Doc, App) 
{ 
	MsgBox,  App Closing
}


wd_NewDocument(Doc,App) { ;when creating a new document, this triggers
    MsgBox, New Document                         ; Show the name of the new document
    }

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Chemomancer, kunkel321, malikye, mikeyww, Nekrocow, Spawnova, theon, Timmy347, vmech and 38 guests