Link Inventor macro with excel macro

Link Inventor macro with excel macro

SPALANIAPPANTN47S
Enthusiast Enthusiast
541 Views
2 Replies
Message 1 of 3

Link Inventor macro with excel macro

SPALANIAPPANTN47S
Enthusiast
Enthusiast

Hi All,

I have open the inventor from excel macro & running another code in invenor macro.

 

Now my question is How to integrate\link the inventor macro with excel macro? 

When we execute the excel macro it should run first excel macro & then inventor macro automatically.

 

Any suggestions..

0 Likes
542 Views
2 Replies
Replies (2)
Message 2 of 3

SPALANIAPPANTN47S
Enthusiast
Enthusiast
0 Likes
Message 3 of 3

WCrihfield
Mentor
Mentor

Here is something that I think should do what you want.

 

The top several lines of this VBA macro code are specifying the names of the 'target' VBAProject, Component (module), & Member (macro).  You will likely have to change these names to match your situation.  The project name is reffering to its 'DisplayName', and is usually something like "ApplicationProject" or "DocumentProject", so if you are looking for an application project among many application projects, you may have to dig deeper to find the right one (same deal with document projects).  The component name is generally the name of the module.  And the member name is generally the name of the macro.  The macro's name is the name of the main procedure within the module.  I have included comments about them within the code.

 

The next section gets the Inventor.Application object, and sets it to a variable, for use later in the code (it should work in Inventor and in Excel, as long as you have the right References turned on).

 

Then it starts attempting to get the actual project, component, & member objects.  I have set-up some fail-safe checks in there to catch when it isn't found.  And I have included some feedback MsgBox's for when they are found, for now, but you will likely want to comment those out once you get it set-up and working, because they can get annoying.  Once it gets the member object (macro), it can execute it.  I have included some comments about that step too. Basically, if your macro is set-up as a Function, you can create a variable just ahead of executing the macro, and put that variable in the () after Execute, to hold the returned value/object from the macro.

 

Here's the code:

 

 

Sub RunOtherMacro()
    Dim oTargetProjectName As String
    oTargetProjectName = "ApplicationProject"
    Dim oTargetProjectFFN As String
    oTargetProjectFFN = "C:\Temp\Target VBA Project.ivb"
    
    Dim oTargetComponentName As String '(name of the Module itself)
    oTargetComponentName = "Export_to_PDF"

    Dim oTargetMacroName As String '(name used in first/main public method within the Module)
    oTargetMacroName = "Export_to_PDF"

    Dim oInvApp As Inventor.Application
    On Error Resume Next
    Set oInvApp = GetObject(, "Inventor.Application")
    If Err.number <> 0 Then
        Set oInvApp = CreateObject("Inventor.Application")
    End If
    If oInvApp Is Nothing Then Exit Sub Else Err.Clear
    On Error GoTo 0
    
    'Dim oInvVBA As Inventor.VbaApplication
    'Set oInvVBA = oInvApp.VbaApplication
    
    Dim oInvVBAProjects As Inventor.InventorVBAProjects
    Set oInvVBAProjects = oInvApp.VBAProjects
    
    'get the target VBA project (can't get it by Item("Name"))
    Dim oProject As Inventor.InventorVBAProject
    Dim oTargetProject As Inventor.InventorVBAProject
    For Each oProject In oInvVBAProjects
        'Call MsgBox("oProject.Name = " & oProject.Name, , "")
        If oProject.Name = oTargetProjectName Then
            Set oTargetProject = oProject
            Exit For
        End If
    Next
    If oTargetProject Is Nothing Then
        Call MsgBox("oTargetProject Is Nothing, so it was not found. Try another project name. Exiting.", , "")
        Exit Sub
    End If
    
    'Call oInvVBAProjects.Open(oTargetProjectFFN)
    
    'get the target module
    Dim oTargetModule As Inventor.InventorVBAComponent
    On Error Resume Next
    Set oTargetModule = oTargetProject.InventorVBAComponents.Item(oTargetComponentName)
    If Err.number <> 0 Then
        Call MsgBox("Error while attempting to get target component directly by Item(Name). Exiting." & vbCrLf & _
        "Err.Number = " & CStr(Err.number) & vbCrLf & _
        "Err.Description = " & Err.Description, , "")
        Exit Sub
    End If
    If oTargetModule Is Nothing Then
        Call MsgBox("oTargetModule Is Nothing, so it was not found. Try another component/module name. Exiting.", , "")
        Exit Sub
    Else
        Call MsgBox("oTargetModule was found!", , "")
        On Error GoTo 0
    End If
        
    'Get the target Macro
    Dim oTargetMacro As Inventor.InventorVBAMember '(the Macro - main/top routine name within the Module)
    On Error Resume Next
    Set oTargetMacro = oTargetModule.InventorVBAMembers.Item(oTargetMacroName)
    If Err.number <> 0 Then
        Call MsgBox("Error while attempting to get target Member (Macro) directly by Item(Name). Exiting." & vbCrLf & _
        "Err.Number = " & CStr(Err.number) & vbCrLf & _
        "Err.Description = " & Err.Description, , "")
        Exit Sub
    End If
    If oTargetMacro Is Nothing Then
        Call MsgBox("oTargetMacro Is Nothing, so it was not found. Try another component/module name. Exiting.", , "")
        Exit Sub
    Else
        Call MsgBox("oTargetMacro was found!", , "")
        On Error GoTo 0
    End If
    
    'If the Macro you are calling is a Function,
    'you can create an Object variable here and supply it within () at the end of the Execute call
    'to capture its return value (It is Optional)
    'Dim oReturned As Object
    'Call oTargetMacro.Execute(oReturned)
    Call oTargetMacro.Execute

    Set oInvApp = Nothing
End Sub

 

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes