How to list all module and sub routine in VBAProject (*.ivb)

How to list all module and sub routine in VBAProject (*.ivb)

tim11_manhhieu
Advocate Advocate
449 Views
5 Replies
Message 1 of 6

How to list all module and sub routine in VBAProject (*.ivb)

tim11_manhhieu
Advocate
Advocate

My idea is to create a userform, and display sub routine and modules on it.

I want to run a sub routine in module by clicking on it.

I just need to know how to list them. I can create userform.

Thanks in advance.

0 Likes
Accepted solutions (1)
450 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Hi @tim11_manhhieu.  I stopped using or relying on VBA in Inventor a couple years ago, but I do have an iLogic rule on hand that you may find helpful and interesting.  It accesses the VBA resources available to Inventor, and digs down into its object/data structure, recording key pieces of information as it goes, then writes all that data out into a text file, sort of like a structured report.  It lists all the 'projects', all the components (aka Modules), all the Members (aka Subs / Functions), and all the Arguments (aka input parameters).

 

This code example does not go into the 'return values' of Members which represent Functions, but when a Member is a Function, you can use its Execute method, which asks for a 'pre-existing' variable as an 'input parameter' that it will then assign the 'return value' to, when it executes.  Since this code example is just exploratory, it does not attempt to Call/Run any Functions, or report about their 'return values'.

That code is within the attached text file, for convenience.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

bradeneuropeArthur
Mentor
Mentor

All modules are already listed under the customization menu found in the ribbon. These can even be added to thevribbon!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 4 of 6

tim11_manhhieu
Advocate
Advocate

I appreciate your help.

I based on your code and thanks to Copilot's help I successfully converted it to vba code.

There are quite a few things to learn in your code.

I will probably learn vb.net language because i want to create add-ins in the future.

 

 

Option Explicit

Sub Main()
    Dim oReport As String
    Dim Tx1 As String
    Dim Tx2 As String
    Dim Tx3 As String
    
'    Dim oVBAProjs As VBAProjects
    Dim oVBAProjs As InventorVBAProjects
    
'    Dim oProj As VBAProject
    Dim oProj As InventorVBAProject
    
'    Dim oComps As VBAComponents
    Dim oComps As InventorVBAComponents
    
'    Dim oComp As VBAComponent
    Dim oComp As InventorVBAComponent
    
'    Dim oMembers As VBAMembers
    Dim oMembers As InventorVBAMembers
    
'    Dim oMember As VBAMember
    Dim oMember As InventorVBAMember
    
'    Dim oArgs As VBAArguments
    Dim oArgs As InventorVBAArguments
    
'    Dim oArg As VBAArgument
    Dim oArg As InventorVBAArgument
    
    Dim oTxtFile As String
'    Dim oWriter As TextStream
    Dim oWriter As Object

'    Set oReport = ""
    oReport = ""
    
    Tx1 = vbTab
    Tx2 = vbTab & vbTab
    Tx3 = vbTab & vbTab & vbTab
    Set oVBAProjs = ThisApplication.VBAProjects

    For Each oProj In oVBAProjs
'        Set oComps = oProj.Components 'Modules
        Set oComps = oProj.InventorVBAComponents
        
        oReport = oReport & "Project Name = " & oProj.name & vbCrLf
        oReport = oReport & "Project Type = " & oProj.ProjectType & vbCrLf
        oReport = oReport & "Components Count = " & oComps.count & vbCrLf

        If oComps.count = 0 Then GoTo SkipProject 'skip to next Project

        For Each oComp In oComps
'            Set oMembers = oComp.Members 'Subs/Functions
            Set oMembers = oComp.InventorVBAMembers
            
            oReport = oReport & Tx1 & "Component Name = " & oComp.name & vbCrLf
            oReport = oReport & Tx1 & "Component Members Count = " & oMembers.count & vbCrLf

            If oMembers.count = 0 Then GoTo SkipComponent 'skip to next Component

            For Each oMember In oMembers
                Set oArgs = oMember.Arguments 'input variables/parameters
                oReport = oReport & Tx2 & "Member Name = " & oMember.name & vbCrLf
                oReport = oReport & Tx2 & "Member Type = " & oMember.MemberType & vbCrLf
                oReport = oReport & Tx2 & "Member Arguments Count = " & oArgs.count & vbCrLf

                If oArgs.count = 0 Then GoTo SkipMember 'skip to next Member

                For Each oArg In oArgs
                    oReport = oReport & Tx3 & "Argument Name = " & oArg.name & vbCrLf
                    oReport = oReport & Tx3 & "Argument Type = " & oArg.ArgumentType & vbCrLf
                Next oArg
SkipMember:
            Next oMember
SkipComponent:
        Next oComp
SkipProject:
    Next oProj

    oTxtFile = "D:\xxxx.txt"
'    If FileExists(oTxtFile) Then Kill oTxtFile

    Set oWriter = CreateObject("Scripting.FileSystemObject").CreateTextFile(oTxtFile)
    oWriter.Write oReport
    oWriter.Close
    
    ShellExecute 0, "open", oTxtFile, "", "", 1  ' 1 means SW_SHOWNORMAL
'    ThisApplication.Documents.Open (oTxtFile)

End Sub

 



0 Likes
Message 5 of 6

tim11_manhhieu
Advocate
Advocate

Thanks for your reply.
I want to create a list module, from which I can sort alphabetically or search by name, or search by working object.

Because my work has quite a lot of manual operations, now my VBA project has about 100 small macros, through many companies I have accumulated that much.

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

@bradeneuropeArthur wrote:

All modules are already listed under the customization menu found in the ribbon. These can even be added to thevribbon!


Actually, the Customize dialog will only show you the ones that it considers true Macros, because those are the only ones allowed for ribbon buttons, radial marking menu buttons, and such.  In Inventor, a true Macro is one that represents a Sub routine that does not have any 'input parameters'.  So, if any of your VBA modules only contain a Sub routine that has any input parameters, then it will not be shown in that list.  That list will also not show any that represent a Function, because Functions are designed to 'Return' a value.  When you click on a ribbon button, there is no way to supply any 'input parameters', and there is no way to 'receive' a return value from it.  The button can only 'Do' a task.  Ribbon controls that are for custom Add-In is another story, and I am not as familiar with how they are regulated by Inventor.

It mentions this in the online help documentation, at the following link:

VBA in Autodesk Inventor (below the second image on that page)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes