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.