callByName gets or sets an object's property or executes an object's method. The name of the property or method are dynamic (i.e. might be stored in a variable.
callByName operates on classes/objects. So, we need to first create a class:
'
' Put this code in a class module and rename it to tq84Obj
'
option explicit
public property get funcOne() as string
funcOne = "this is the first function"
end property
public property get funcTwo() as string
funcTwo = "this is the second function"
end property
public sub subOne()
debug.print "first sub was called"
End Sub
public sub subTwo()
debug.print "second sub was called"
end sub
Now, we can call methods/subs etc on the class with callByName:
option explicit
sub main()
dim obj as tq84Obj
set obj = new tq84Obj
dim res as string
debug.print CallByName(obj, "funcOne", vbGet)
debug.print CallByName(obj, "funcTwo", vbGet)
callByName obj, "subOne", vbMethod
callByName obj, "subTwo", vbMethod
debug.print res
end sub