Here is a question we often get from ADN support:
My Inventor documents contain embedded VBA code. I no longer need them. Is there any way to remove the VBA code programmatically?
Solution
You can access the VBA object model using Document.VBAProject property. The code to remove VBA code for active document is exposed below (VB.Net):
Note: Need to add a reference to "Microsoft Visual Basic for Applications Extensibility 5.3"
Sub RemoveVBA()
Dim m_inventorApp As Inventor.Application = Nothing
' Try to get an active instance of Inventor
Try
m_inventorApp = System.Runtime.InteropServices. _
Marshal.GetActiveObject("Inventor.Application")
Catch ex As Exception
' If not active, create a new Inventor session
If m_inventorApp Is Nothing Then
Dim inventorAppType As Type = _
System.Type.GetTypeFromProgID("Inventor.Application")
m_inventorApp = _
System.Activator.CreateInstance(inventorAppType)
End If
End Try
Dim oDoc As Document
oDoc = m_inventorApp.ActiveDocument
Dim oVBComp As VBComponent
Dim i As Integer
For i = oDoc.VBAProject.VBProject.VBComponents.Count To 1 Step -1
oVBComp = oDoc.VBAProject.VBProject.VBComponents(i)
If oVBComp.Type <> vbext_ComponentType.vbext_ct_Document Then
Call oDoc.VBAProject.VBProject.VBComponents.Remove(oVBComp)
End If
Next i
' remove lines
oVBComp = oDoc.VBAProject.VBProject.VBComponents(1)
oVBComp.CodeModule.DeleteLines(1, oVBComp.CodeModule.CountOfLines)
End Sub