7 Replies Latest reply on Feb 4, 2017 9:21 PM by Keith Rice

    Delete all "file not found" components in an assembly.

    Andy Rammer

      Does anyone know of a macro that will traverse the top level assembly feature tree and delete any components that were suppressed on open because of missing references?

        • Re: Delete all "file not found" components in an assembly.
          Solid Air

          There probably is but it would be something that if your interested in learning VBA that should not be too hard to write.  There are examples for traversing the tree, all you need do is add a deletion routine.

          • Re: Delete all "file not found" components in an assembly.
            Keith Rice



            This could be done a lot of ways but this is possibly the simplest. It simply looks to see if a component's file path exists. If not, it deletes that component. To use the code below, you will need to add a reference to Microsoft Scripting Runtime via Tools-->References within the VB Editor.


            '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
            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
                    End If
                Next i
            End Sub


            Keith Rice

            SolidWorks API Training and Services