Exporting entire VBA projects to compare version changes

Exporting entire VBA projects to compare version changes

Anonymous
Not applicable
656 Views
1 Reply
Message 1 of 2

Exporting entire VBA projects to compare version changes

Anonymous
Not applicable

I'm using Inventor 2015 

 

I'd like to create a VB.NET app that can compare different versions of an entire VBA project.

 

To do this I would like to programmically export an entire VBA project in one go.

 

In VB.NET I imported the Microsoft VBA Extensibilty class,

 

 

but I have no way to connect it to the stand alone External VBA project file that inventor uses.

 

Normally with office products I could make a reference to say an Excel or Word document then

Connect my VBE object to its VBA project object.

 

Is there any sample code available to export the code from Inventors External VBA project?

 

 

 

0 Likes
Accepted solutions (1)
657 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

Issue resolved.

 

Here is the final generic VBA tool I came up with.
Feel free to use it if necessary.
With MS Office docs you do not need to go to these lengths as VBA is saved inside the Office docs.

With Inventor and Autocad they are External files.

VBA code follows

Code:
Option Explicit

Public Sub DumpAllVbaProjectCode()
  ' This utility was written for 64 bit Inventor 2015. Untested with 32 bit.
  ' Make sure to reference VBE (Microsoft Visual Basic for Applications Extensibility 5.3
  
  ' You may have to browse to the file containing the reference:
  ' C:\Program Files (x86)\Common Files\Microsoft Shared\VBA\VB6\VBE6EXT.OLB

  Const vbext_ct_StdModule = 1
  Const vbext_ct_ClassModule = 2
  Const vbext_ct_MSForm = 3
  Const vbext_ct_Document = 100
  
  Dim oModule As InventorVBAComponent
  Dim sOut As String
  
  sOut = ""
  For Each oModule In ThisApplication.VBAProjects(1).InventorVBAComponents
    With oModule.VBComponent
      If .Type >= vbext_ct_StdModule And .Type <= vbext_ct_Document Then
        
        Dim PadLine As String
        If sOut = "" Then
          PadLine = ""
        Else
          PadLine = vbCrLf
        End If
        
        sOut = sOut & PadLine & "' \\\ " & oModule.Name & " ///" & vbCrLf
        
        Dim oCodeModule As CodeModule
        Set oCodeModule = oModule.VBComponent.CodeModule
        
        sOut = sOut & CleanString(oCodeModule.Lines(1, oCodeModule.CountOfLines)) & vbCrLf
      End If
    End With
  Next

  Open "C:\VBA_Dump.txt" For Output As #1
    Print #1, sOut
  Close #1

  MsgBox "VBA Project Dumped"
End Sub

Public Function CleanString(STR As String) As String
  ' Formalizes the output so it may be compared with other versions of the project.
  Dim sLines() As String
  Dim sOut As String
  Dim i As Integer
  
  ' remove tabs
  STR = Replace(STR, vbTab, "")

  sLines = Split(STR, vbCrLf)
  For i = 0 To UBound(sLines) - 1
    sLines(i) = RTrim(sLines(i))
    If Not (sLines(i) = RTrim(sLines(i + 1)) And sLines(i) = "") Then
      ' construct new string
      sOut = sOut & sLines(i) & vbCrLf
    End If
  Next i

  Do
    ' Clean up trailing newlines
    If Right$(sOut, 2) = vbCrLf Then
      sOut = Left$(sOut, Len(sOut) - 2)
    End If
  Loop Until Right$(sOut, 2) <> vbCrLf
  
  CleanString = Trim(sOut)
End Function
0 Likes