Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Passing a function name to another function

2 REPLIES 2
Reply
Message 1 of 3
pball
310 Views, 2 Replies

Passing a function name to another function

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.

2 REPLIES 2
Message 2 of 3
Anonymous
in reply to: pball

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

 

 

Message 3 of 3
ekinsb
in reply to: pball

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

 

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report