9 Replies Latest reply on Jun 29, 2018 12:03 AM by Fifi Riri

    Basic component replacement macro...

    Mo Thao

      Hi all, new to the forums so I apologize if this is a duplicate.  Does anyone know of a macro I can  batch run on 80 drawings to replace a component part?  I tried creating one from scratch recording it to no success.  I searched the forums and didn't find any questions/answers.

       

      Thanks, looking forward to contributing as I'm usually in the Autodesk forums.

       

      -Mo-

        • Re: Basic component replacement macro...
          Fifi Riri

          Assuming all assemblies and their sub assemblies are in the same folder "MyFolder" (otherwise you will have to write a TraverseAssembly function).

          This will open every one of them, replace component "Part1" by "Part2", then close and save.

           

          Dim swApp As SldWorks.SldWorks

          Dim swAssy As SldWorks.AssemblyDoc

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

          Dim FolderPath As String, PartToReplace As String, PathNewPart As String

           

          Sub main()

               FolderPath = "C:\MyFolder\"

               PartToReplace = "Part1"

               PathNewPart = "C:\PartFolder\Part2.SLDPRT"

               Set swApp = Application.SldWorks

               Dim StrFile As String

               StrFile = Dir(FolderPath & "*.sldasm")

               Do While Len(StrFile) > 0

                  ReplacePart (FolderPath & StrFile)

                  StrFile = Dir

               Loop

          End Sub

           

          Sub ReplacePart(ByVal AssyPath As String)

               Dim vComponents As Variant, vComp As Variant

               Dim swComp As SldWorks.Component2

               Dim Filename As String

               Set swAssy = swApp.OpenDoc6(AssyPath, 2, 0, "", longstatus, longwarnings)

               vComponents = swAssy.GetComponents(True)

               Filename = swAssy.GetTitle

               Filename = Left(Filename, InStr(Filename, ".") - 1)

               For Each vComp In vComponents

                   Set swComp = vComp

                   If InStr(swComp.Name2, PartToReplace) > 0 Then

                       boolstatus = swAssy.Extension.SelectByID2(swComp.Name2 & "@" & Filename, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

                       boolstatus = swAssy.ReplaceComponents(PathNewPart, "", True, True)

                   End If

               Next

               boolstatus = swAssy.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, longstatus, longwarnings)

               swApp.CloseDoc swAssy.GetTitle

          End Sub

            • Re: Basic component replacement macro...
              Mo Thao

              Unfortunately they do reside in a different folder (was moved up on folder).  Any chance you had a TraverseAssembly function handy?

               

              And thanks for the reply, much appreciated Fifi.

                • Re: Basic component replacement macro...
                  Fifi Riri

                  There is 2 problems here:

                  1. Do your assemblies have sub-assemblies? The TraverseAssembly function would only be useful to find sub-assemblies.
                  2. What's link the 80 assemblies? Are they in the same folder? Are they sub-assemblies of 1 assembly?
                    • Re: Basic component replacement macro...
                      Mo Thao

                      Both old and new parts are not subassemblies or saved as assemblies.  And both don't have any sub-assemblies.

                      Both are located at path: J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS

                       

                      Part 076352.sldprt is being replaced with 078997.sldprt

                       

                      Hopefully that makes sense.

                        • Re: Basic component replacement macro...
                          Mo Thao

                          Oh, just reread your post...not sure how to edit post, but the assemblies that contain the parts are located in the same path as noted above.

                           

                          The top level assemblies names are:

                          098653, 098654, 098655, 098656, 098657, 098658, 098659, 098660, 098661, 098662, 098663, 098664, 098665, 098666, 098667, 098668, 098669, 098670, 098671, 098672, 098673, 098674, 098675, 098676, 098677, 098678, 098679, 098680, 098681, 098682, 098683, 098684, 098685, 098686, 098687, 098688, 098689, 098690, 098691, 098692, 098693, 098694, 098695, 098696, 098697, 098698, 098699, 098700, 098701, 098702, 098703, 098704, 098705, 098706, 098707, 098708, 098709, 098710, 098711, 098712, 098713, 098714, 098715, 098716, 098719, 098720, 098721, 098722, 098723, 098724, 098725, 098726, 098727, 098728, 098729, 098730, 098731, 098732, 098733, 098734, 098735, 098736, 098737, 098738, 098747

                            • Re: Basic component replacement macro...
                              Mo Thao

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098710.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098711.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098712.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098713.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098653.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098654.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098655.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098656.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098657.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098658.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098659.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098660.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098661.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098662.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098663.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098664.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098665.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098666.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098667.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098668.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098669.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098670.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098671.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098672.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098673.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098674.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098675.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098676.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098677.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098678.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098679.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098680.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098681.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098682.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098683.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098684.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098685.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098686.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098687.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098688.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098689.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098690.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098691.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098692.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098693.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098694.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098695.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098696.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098697.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098698.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098699.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098700.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098701.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098702.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098703.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098704.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098705.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098706.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098707.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098708.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098709.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098714.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098715.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098716.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098738.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098719.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098720.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098721.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098722.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098723.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098724.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098725.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098726.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098727.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098728.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098729.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098730.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098731.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098732.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098733.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098734.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098735.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098736.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098737.SLDASM"

                              "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\098747.SLDASM"

                                • Re: Basic component replacement macro...
                                  Fifi Riri

                                  Ok but my first question still stand: Do your assemblies have subassemblies?

                                   

                                  if not, just edit the lines as so:

                                   

                                  Sub main()

                                      FolderPath = "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\"

                                      PartToReplace = "076352"

                                      PathNewPart = "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\078997.sldprt"

                                      Set swApp = Application.SldWorks

                                      Dim StrFile As String

                                      StrFile = Dir(FolderPath & "*.sldasm")

                                      Do While Len(StrFile) > 0

                                           If Val(replace(LCase(StrFile), ".sldasm", "")) > 98652 and Val(replace(LCase(StrFile), ".sldasm", "")) < 98748 Then

                                                 ReplacePart (FolderPath & StrFile)

                                           End If

                                            StrFile = Dir

                                      Loop

                                  End Sub

                                    • Re: Basic component replacement macro...
                                      Mo Thao

                                      Yes, there are sub-assemblies.

                                        • Re: Basic component replacement macro...
                                          Fifi Riri

                                          This actually works with sub-assemblies too. Just try it.

                                           

                                          Dim swApp As SldWorks.SldWorks

                                          Dim swAssy As SldWorks.AssemblyDoc

                                          Dim boolstatus As Boolean

                                          Dim longstatus As Long, longwarnings As Long

                                          Dim FolderPath As String, PartToReplace As String, PathNewPart As String

                                           

                                          Sub main()

                                              FolderPath = "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\"

                                              PartToReplace = "076352"

                                              PathNewPart = "J:\Design Internal\DFM\ENG Design\ERP\ERP SOLIDWORKS\078997.sldprt"

                                              Set swApp = Application.SldWorks

                                              Dim StrFile As String

                                              StrFile = Dir(FolderPath & "*.sldasm")

                                              Do While Len(StrFile) > 0

                                                   If Val(replace(LCase(StrFile), ".sldasm", "")) > 98652 and Val(replace(LCase(StrFile), ".sldasm", "")) < 98748 Then

                                                         ReplacePart (FolderPath & StrFile)

                                                   End If

                                                    StrFile = Dir

                                              Loop

                                          End Sub

                                           

                                          Sub ReplacePart(ByVal AssyPath As String)

                                               Dim vComponents As Variant, vComp As Variant

                                               Dim swComp As SldWorks.Component2

                                               Dim Filename As String

                                               Set swAssy = swApp.OpenDoc6(AssyPath, 2, 0, "", longstatus, longwarnings)

                                               vComponents = swAssy.GetComponents(True)

                                               Filename = swAssy.GetTitle

                                               Filename = Left(Filename, InStr(Filename, ".") - 1)

                                               For Each vComp In vComponents

                                                   Set swComp = vComp

                                                   If InStr(swComp.Name2, PartToReplace) > 0 Then

                                                       boolstatus = swAssy.Extension.SelectByID2(swComp.Name2 & "@" & Filename, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

                                                       boolstatus = swAssy.ReplaceComponents(PathNewPart, "", True, True)

                                                   End If

                                               Next

                                               boolstatus = swAssy.Save3(swSaveAsOptions_e.swSaveAsOptions_Silent, longstatus, longwarnings)

                                               swApp.CloseDoc swAssy.GetTitle

                                          End Sub