Inventor VBA - Enable Reference by Code

Inventor VBA - Enable Reference by Code

Anonymous
Not applicable
2,712 Views
2 Replies
Message 1 of 3

Inventor VBA - Enable Reference by Code

Anonymous
Not applicable

Good morning all,

I've got the following problem and I'm hoping that someone has an idea how to solve this:

 

I implemented a solution for knowledge based engineering (KBE) which consists of some parametrized Inventor parts, a lot of VBA Code and a MS SQL Server. To connect to the MS SQL Server I use the VBA Reference "Microsoft ActiveX Data Object 2.7". This can be enabled manually in the VBA Editor.

My question in the view of deploying this solution throughout 100 workstations is: How to automate this enabling of the reference, e.g. by coding ?

I found many solutions for MS Excel VBA (ThisWorkbook.Reference.AddByPath) but not a single Inventor compatible solution.

 

Any recommendations ?

 

Greetings from Germany

 

Julian

0 Likes
2,713 Views
2 Replies
Replies (2)
Message 2 of 3

Vladimir.Ananyev
Alumni
Alumni

Hope you can do it via the Inventor VBA.  The following snippet prints names of all references in your VBA project:

    'Get the default VBA project.
    Dim oVBAprojects As InventorVBAProjects
    Set oVBAprojects = ThisApplication.VBAProjects
   
    Dim vbProj As InventorVBAProject
    Set vbProj = oVBAprojects.Item(1)
 
    Dim refList As Object
    Set refList = vbProj.VBProject.References
   
    Dim chkRef As Object
    For Each chkRef In vbProj.VBProject.References
        Debug.print chkRef.name
    Next

And you may add references to the VBA project:

Call vbProj.VBProject.References.AddFromGuid("{00000000-0000-0000-0000-000000000000}", 5, 0)

 detailes are here:

https://msdn.microsoft.com/en-us/library/aa443980(v=vs.60).aspx

https://msdn.microsoft.com/en-us/library/aa443702(v=vs.60).aspx

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 3 of 3

ekinsb
Alumni
Alumni

Hi Julian,

 

I see three possible solutions to this.  The first two don't require adding any references.

 

1. Several customers that I know of have an ivb file that they create and use company wide.  When they write new utilities in VBA, they create them in this project and then deploy the updated ivb file to the user's computers (or update a single shared version of the file).  In this case you would just add the reference to this project and then everyone gets it automatically.

 

2. You can use "late binding".  With this approach, instead of declaring variables to be of a specific type you can declare them to be of type "Object".  At runtime, VBA will get the object returned and perform "late binding" to figure out what type of object it is and what functions the object supports.  All of your code should work as expected.  The downside to this approach is that while your writing your code you don't get any code hints and at runtime the execution will be slightly slower.  But I would guess the performance hit in your case won't even be noticable.

 

3. From the Inventor API you can access the API for the VBA Development Environment.  Using that API you can add references.  Below is a small test case I wrote that does this.  It defines the reference by specifying the full path to the dll you want to reference.  There is also an AddFromGuid method that you can use if that's easier in your case.  One thing to be aware of is that to be able to use any of the classes defined in the VBIDE library I had to first reference the "Microsoft Visual Basic for Applications Extensibility 5.3" library.  This is because I declared them as specific objects from the VBIDE library.  Instead, I could use late binding for those objects, like what I discussed above.

 

Public Sub AddReference()
    Dim invVBAProj As Inventor.InventorVBAProject
    Set invVBAProj = ThisApplication.VBAProjects.Item(1)
    
    Dim VBAProj As VBIDE.VBProject
    Set VBAProj = invVBAProj.VBProject
    
    ' Ignore any errors because it's throwing an exception even when it succeeds.
    On Error Resume Next
    Dim path As String
    path = "C:\Windows\System32\cryptext.dll"
    Call VBAProj.References.AddFromFile("C:\Windows\System32\cryptext.dll")
    On Error GoTo 0
    
    ' Check the reference exists.
    Dim ref As VBIDE.Reference
    Dim succeeded As Boolean
    succeeded = False
    For Each ref In VBAProj.References
        If UCase(ref.FullPath) = UCase(path) Then
            succeeded = True
            Exit For
        End If
    Next
    
    If Not succeeded Then
        MsgBox "Failed to add referenced to """ & path & """"
    End If
End Sub

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