3 Replies Latest reply on Feb 22, 2018 3:08 PM by Attilio Colangelo

    Macro to rename selected parts in assembly

    Tomas Pruha

      Hello, I am working on macro which would rename selected parts in assembly. Currently i have this:

       

      Dim swApp As Object

      Dim swModel As Object

      Dim swModelDocExt As Object

      Dim swSelMgr As Object

      Dim nbrSelections As Long

      Dim thisObject As Object

      Dim thisPartName As String

      Dim thisPart As Object

      Dim errorsRename As Long

       

      Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swSelMgr = swModel.SelectionManager

          nbrSelections = swSelMgr.GetSelectedObjectCount2(-1)

          nbrSelections = nbrSelections - 1

          Debug.Print nbrSelections

         

          For i = 0 To nbrSelections

              Set thisObject = swSelMgr.GetSelectedObject6(i + 1, -1)

              thisPartName = thisObject.Name

              Debug.Print thisPartName

              Set thisPart = swModel.GetComponentByName(thisPartNameName)

              errorsRename = thisPart.Extension.RenameDocument("Renamed")

          Next

         

      End Sub

       

      But it throws Run-time error "91". on this line:

       

      errorsRename = thisPart.Extension.RenameDocument("Renamed")

       

      What am I doing wrong?

        • Re: Macro to rename selected parts in assembly
          Attilio Colangelo

          A couple things:

           

          - You have a typo

          swModel.GetComponentByName(thisPartNameName) should be

          swModel.GetComponentByName(thisPartName)

           

          - Extension is a property on ModelDoc2 so make the following changes:

          Dim thisPart As Component2 'Highly recommend declaring this up front

          Set thisPart = swModel.GetComponentByName(thisPartName)

          errorsRename = thisPart.GetModelDoc2.Extension.RenameDocument("Renamed")

           

          I have tested it and seems to work.

            • Re: Macro to rename selected parts in assembly
              Tomas Pruha

              Thank you, now this code throws no errors, but it does nothing :/ I added debug print to see where is the error and it seems to be in renaming line

               

              Debug.Print "Rename document errors: " & errorsRename

              prints this line:

              Rename document errors: 2

               

              My full code:

               

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swSelMgr As Object

              Dim nbrSelections As Long

              Dim thisObject As Object

              Dim thisPartName As String

              Dim thisPart As Component2

              Dim errorsRename As Long

               

              Sub main()

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSelMgr = swModel.SelectionManager

                  nbrSelections = swSelMgr.GetSelectedObjectCount2(-1)

                  nbrSelections = nbrSelections - 1

                  Debug.Print nbrSelections

                 

                  For i = 0 To nbrSelections

                      Set thisObject = swSelMgr.GetSelectedObject6(i + 1, -1)

                      thisPartName = thisObject.Name

                      Debug.Print thisPartName

                      Set thisPart = swModel.GetComponentByName(thisPartName)

                      errorsRename = thisPart.GetModelDoc2.Extension.RenameDocument("Renamed")

                      Debug.Print "Rename document errors: " & errorsRename

                  Next

                 

              End Sub

                • Re: Macro to rename selected parts in assembly
                  Attilio Colangelo

                  Well breaking down the docs for  RenameDocument (emphasis added)

                  Temporarily renames the selected component using the specified name

                   

                  So we need to select it first using (I guess "getting" using GetComponentByName isn't enough?)

                  status = thisPart.GetModelDoc2.Extension.SelectByID2(thisPartName + "@beam_boltconnection", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

                   

                  This was from my test model so you have to adjust for your assembly name, etc.  You need to return a status TRUE from this SelectByID2 method and then

                  errorsRename = thisPart.GetModelDoc2.Extension.RenameDocument("Renamed")

                   

                  should execute successfully.