AnsweredAssumed Answered

VBA - Macro Replace Component Function Question

Question asked by Cad Admin on May 24, 2017
Latest reply on May 30, 2017 by Cad Admin

I putting together a macro that i can run on an assembly to automatically replace parts.  Basically im replacing a specific named part with a identical part with a different name from a defined standards library location.


I can get the components & their configurations being used.  However the "replace" is failing

my problem lies in:  bRet = swAssy.ReplaceComponents2(FileName, CfgName, True, 0, True), need it to return true & im getting false


See Code Below:


Option Explicit


Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)


    Dim swApp                   As SldWorks.SldWorks

    Dim swModel                 As SldWorks.ModelDoc2

    Dim swAssy                  As SldWorks.AssemblyDoc

    Dim swSelMgr                As SldWorks.SelectionMgr

    Dim swSelComp               As SldWorks.Component2

    Dim swSelModel              As SldWorks.ModelDoc2

    Dim vChildComp              As Variant

    Dim swChildComp             As SldWorks.Component2

    Dim sPadStr                 As String

    Dim i                       As Long

    Dim CfgName                 As String

    Dim FileName                As String

    Dim bRet                    As Boolean

    Dim nErrors                 As Long


    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swAssy = swModel

    Set swSelMgr = swModel.SelectionManager


    For i = 0 To nLevel - 1

        sPadStr = sPadStr + "  "

    Next i

    vChildComp = swComp.GetChildren

    For i = 0 To UBound(vChildComp)

        Set swChildComp = vChildComp(i)

        CfgName = swChildComp.ReferencedConfiguration

        Debug.Print sPadStr & "Component name: " & swChildComp.Name2 & ", Component ID: " & swChildComp.GetID

        Debug.Print sPadStr & "Component name: " & CfgName


'-----------------------------------Start Standard INCH Parts---------------------------------------

                        If swChildComp.Name2 Like "*INCH-9752*" Then

                            FileName = "D:\Inch\INCH-9752-15665.sldprt"

                        End If


                        If swChildComp.Name2 Like "*INCH-9566*" Then

                            FileName = "D:\Inch\INCH-9566-15756.sldprt"

                        End If

'-----------------------------------End Standard INCH Parts---------------------------------------

                    bRet = swAssy.ReplaceComponents2(FileName, CfgName, True, 0, True)

                    Debug.Print "Replacement component = " & swChildComp.Name2

                    Debug.Print "All instances of old component replaced? " & bRet


        TraverseComponent swChildComp, nLevel + 1


    Next i

End Sub


Sub main()


    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swConfMgr                   As SldWorks.ConfigurationManager

    Dim swConf                      As SldWorks.Configuration

    Dim swRootComp                  As SldWorks.Component2

    Dim swSelMgr                    As SldWorks.SelectionMgr


    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swConfMgr = swModel.ConfigurationManager

    Set swConf = swConfMgr.ActiveConfiguration

    Set swRootComp = swConf.GetRootComponent3(True)

    'Debug.Print "File = " & swModel.GetPathName

    If swModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then

        TraverseComponent swRootComp, 1

    End If


End Sub