Removing VBA Document Projects From Inventor Files

I received an email recently with a question that relates back to a series of past postings in this blog.  They used the previous blog postings to migrate their document macros to an add-in but are now wondering how to remove their document macros from their files.  There are three approaches that I'm aware of to do this.  With all of these make sure you have a back up of the file before deleting the macros.

First, you can manually open each document and use the VBA editor to delete the code from the document project.  This is fine if you only have a handful of documents but is tedious if you have many documents.  The advantages to this approach is that it's simple and also gives you the flexibility to look at the macros within the document and decide one-by-one whether to delete and/or to copy the macro to the application project.

Second, you can use a VBA macro to automate the deletion.  Here's a VBA macro that I wrote using the functionality provided by Inventor's API and the VBA Extensibility library to manipulate a VBA project.  It either comments or deletes all automatic macros from the specified VBA project.  You could easily wrap this to open, process, and save many documents.  This will leave any other macros in the project alone.  There are some cases where you might have a regular macro (vs. an auto macro) that is specific to that document and you don't want to delete it.  To use the macro below, copy and paste it into your application project and add a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library.

' Sub to test the DeleteAutoMacros sub.
Public Sub TestDeleteAutoMacros()
    Dim oActiveDocument As Document
    Set oActiveDocument = ThisApplication.activeDocument
    Call DeleteAutoMacros(oActiveDocument.VBAProject, True)
End Sub

' Given an Inventor VBA project as input, this
' deletes or comments the code for all of the
' automatic macros contained within the project.
Private Sub DeleteAutoMacros( _
                         ByVal VBAProject As InventorVBAProject, _
                         Optional ByVal Comment As Boolean = True)
   ' Iterate through the modules of the document project.
   Dim oModule As InventorVBAComponent
   For Each oModule In VBAProject.InventorVBAComponents
      ' Check to see if this is a code module or document module.
      If oModule.VBComponent.Type = vbext_ct_StdModule Or _
         oModule.VBComponent.Type = vbext_ct_Document Then
         ' Look for any auto macros.
         Dim oFunction As InventorVBAMember
         For Each oFunction In oModule.InventorVBAMembers
            ' Check that this is an automatic macro.
            Select Case LCase(oFunction.Name)
            Case "autosave", "autoopen", "autonew", _
                 "autoclose", "autoedit"
               ' Get the VBIDE object that represents the module.
               Dim oVBCodeModule As codeModule
               Set oVBCodeModule = oModule.VBComponent.codeModule

               ' Get the start line and length of the module.
               Dim iStartLine As Long
               iStartLine = oVBCodeModule.ProcStartLine( _
                                  oFunction.Name, vbext_pk_Proc)
               Dim iNumLines As Long
               iNumLines = oVBCodeModule.ProcCountLines( _
                                  oFunction.Name, vbext_pk_Proc)

               If Comment Then
                  ' Get the macro as a string.
                  Dim strMacroLines As String
                  strMacroLines = oVBCodeModule.Lines( _
                                           iStartLine, iNumLines)

                  ' Insert a comment at the beginning line.
                  strMacroLines = "' " & strMacroLines

                  ' Add a comment at the beginning of each line.
                  strMacroLines = Replace(strMacroLines, _
                                       vbCrLf, vbCr & "' ")

                  ' Delete the existing macro.
                  Call oVBCodeModule.DeleteLines(iStartLine, _
                                                 iNumLines)

                  ' Add the commented version of the macro.
                  Call oVBCodeModule.InsertLines(iStartLine, _
                                                 strMacroLines)
               Else
                  ' Delete the lines.
                  Call oVBCodeModule.DeleteLines(iStartLine, _
                                                 iNumLines)
               End If
            End Select
         Next
      End If
   Next
End Sub

The third option is to use this Delete VBA utility to completely remove the VBA project from the document.  It's a command line utility.  Just supply the filename of the Inventor file as an argument when run from the command line.  The zip file in the link contains the program and the source code.  To use it you only need the DeleteVBA.exe file.  If you're sure you don't have any other VBA code in the document that you care about this is the best option.


Comments

4 responses to “Removing VBA Document Projects From Inventor Files”

  1. Jayaram Raman Avatar
    Jayaram Raman

    The command prompt utility comes with the following message:
    A filename must be specified in the command line.
    On clicking OK The command utility promptly disappears.
    Anything I’m doing wrong?
    Thanks

  2. You need to run the program from the Command window where you can specify additional options. In this case you need to specify the name of the Inventor file to remove the VBA project from.
    To open a Command window you can use the “Run…” command in the Start menu and type in “cmd” as the command to run. This will open the command window. Change directory to wherever you downloaded the utility to (C:\Temp\VBARemove in the example below).
    C:\Users\Ekins> cd C:\Temp\VBARemove
    Run the utility specifying the filename.
    C:\Temp\VBARemove> DeleteVBA.exe C:\Users\Ekins\Part1.ipt

  3. Wolfgang Seidel Avatar
    Wolfgang Seidel

    After using DeleteVBA.exe all macros were deleted. We did this directly on a copy of the file store. The only problem that remains is that we get strange vault status in the Autodesk Vault:
    E.g. some of the files have green staus flags that do not get white if you check out this file.
    Does DeleteVBA.exe also influence the status flags? Do you know what to do about this problem?

  4. Rob Beckley Avatar
    Rob Beckley

    I need to delete some code from all our files, I tried your DeleteAutoMacros but it errors on ‘oVBCodeModule As codeModule’ saying User-defined type not defined
    We only have one bit of code and it dose not work because of the ThisDocument. ref in 64bit windows.

    Public Sub AutoSave()
    Dim oCustomPropSet As PropertySet
    Set oCustomPropSet = ThisDocument.PropertySets.Item(“{D5CDD505-2E9C-101B-9397-08002B2CF9AE}”)
    ‘ Create or update the property containing the Total Mass.
    Dim strTotalMass As String
    strTotalMass = Format(ThisDocument.ComponentDefinition.MassProperties.Mass, “###0.000”)
    On Error Resume Next
    oCustomPropSet.Item(“Total Mass”).Value = strTotalMass
    If Err Then
    Err.Clear
    Call oCustomPropSet.Add(strTotalMass, “Total Mass”)
    End If
    On Error GoTo 0
    End Sub

    Not sure where its going wrong?

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading