I'm trying to be able to have a function run another function with the secondary function being passed as an argument to the first. I'm trying to create a recursive function and the recursive part works great. I just need to be able to have the recursive function run other functions to actually do stuff.
Here is something I found that should do what I need, mainly run a function from a function name in a variable.
Sub test() Dim ftnName As String Dim argument As String Dim result As String ftnName = "myFunction" argument = "cat" result = Application.Run(ftnName, argument) MsgBox result End Sub Function myFunction(inString As String) As String myFunction = inString & " has " & Len(inString) & " letters." End Function
However I get the error "object required" on the "result = Application.Run(ftnName, argument)" line. So I'm guessing the Inventor VBA doesn't like this method of doing this. I'm just hoping there is a way to do this or this function will be useless or reduced to hardcoding functions which still defeats the purpose.
Hi pball,
I'm unsure how to do it the way you ask... but a work around method could be creating you're code in an iLogic Rule, and proceeding to call a Rule from within another iLogic Rule (which is definitely possible).
This would be the same concept where you have the body of your code in one Rule, and call another Rule which consists of your recursion functions.
You can pass variables back and forth by using parameters, filling them out as needed and changing them that way.
I realize its not whata you were looking for, but simply I thought if no one responds with the proper way.
Regards
Mitch
The Application object doesn't support a Run method however it is possible to do what you want but it's a bit more involved. The API does support accessing a VBA project and it's various modules and executing functions. Below is a modified version of your program that calls the function successfully.
Sub test() Dim ftnName As String Dim argument As String Dim result As Variant ftnName = "myFunction" argument = "cat" ' Get the first project from the collection. ' This will be the default application project. Dim project As InventorVBAProject Set project = ThisApplication.VBAProjects.Item(1) Dim module As InventorVBAComponent Set module = project.InventorVBAComponents.Item("Module1") Dim func As InventorVBAMember Set func = module.InventorVBAMembers.Item(ftnName) func.Arguments.Item(1).Value = argument Call func.Execute(result) MsgBox result End Sub Function myFunction(inString As String) As String myFunction = inString & " has " & Len(inString) & " letters." End Function