I need make use of several Windows API
functions in a Class that I am developing, for a hobby project. Several of these functions require the use of the AddressOf
Operator, but as per Microsoft Documentation, its use in a Class Module is prohibited.
Does anyone know of a function or some standard method that can emulate the the AddressOf
Operator, or is that even possible?
Background
The app centers around functions that are called from the worksheet, which are then used to instantiate a class and call a method using the SetTimer
WinAPI function.
You might say: "Well, you could just use Application.OnTime
", and you would be right, IF the function was NOT called from the worksheet. For good reason, Excel's calculation engine explicitly ignores calls to Application.OnTime
, (if the caller be the worksheet); however, SetTimer
happens to work regardless.
I want to avoid the clunky implementation of placing a public function in a standard module, (which would be dependent on an instance of the class), where I WOULD be able to use the AddressOf
Operator, albeit in an ugly, un-encapsulated way.
Edit: As mentioned in the comments, initially, I intentionally did not disclose exactly what I was trying to do to avoid hearing "you shouldn't do that", lol. I have a working class that does exactly what I want it to do, (i.e. return arrays to the worksheet using the standard method of Ctrl+Shift+Enter), but I wanted to try and emulate the Dynamic Array Functions that are currently being beta tested by the Excel dev team, which do not require you select the range and enter an array via Ctrl+Shift+Enter. I knew if I asked something like "how can I return an array to the WorkSheet from a UDF without Ctrl+Shift+Enter", everyone would provide existing answers and/or shame me, (I would do the same if someone else asked, lol), for asking how to implement something that contradicts the way Excel's calculation engine was intended to function.
Saying that, I also have yet another version of my class that uses the QueryTable
object to place data in the sheet and works much like the Dynamic Array Functions
. I am probably going to post each implementation on Code Review to see how I could improve them/gain some insight to which would be the most stable implementation, etc.
Private Declare Function SetTimer Lib "user32" _
(ByVal HWnd As Long, ByVal nIDEvent As Long,
ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Function Method1(varValsIn As Variant) As Variant
Dim lngWindowsTimerID As Long
'doing some stuff
'call API function after doing some stuff
lngWindowsTimerID = SetTimer(0&, 0&, 1, AddressOf DoStuff)
End Sub
Private Sub DoStuff
'Stuff to do
End Sub