MACRO LIBRARY

Delete all missing components

Free public access. No account or subscription required.

Files

Delete all missing components

Code

Module: mMain

'Deletes all missing components in an assembly
    
'Preconditions: Assembly is open.
    
'Note: You can control whether this macro affects only top level assembly
'   components via the TOP_LEVEL_ONLY constant.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Const TOP_LEVEL_ONLY As Boolean = True
    
Sub main()
    Dim fso As New FileSystemObject
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swAssy As SldWorks.AssemblyDoc
    Dim swComp As SldWorks.Component2
    Dim vComps As Variant
    Dim i As Integer
      
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
      
    If swModel Is Nothing Then Exit Sub
    If swModel.GetType <> swDocASSEMBLY Then Exit Sub
      
    Set swAssy = swModel
    vComps = swAssy.GetComponents(TOP_LEVEL_ONLY)
      
    If IsEmpty(vComps) Then Exit Sub
      
    For i = 0 To UBound(vComps)
        Set swComp = vComps(i)
        
        If fso.FileExists(swComp.GetPathName) = False Then
            swComp.Select False
            swModel.EditDelete
        End If
    Next i
End Sub