3 Replies Latest reply on Jan 26, 2018 6:38 AM by Grant Kirkland

    How to batch rename configurations?

    Grant Kirkland

      I'm trying to write a VBA macro to iterate through all configurations and rename (remove characters from existing string).

       

      For example, I want

       

      0.2500-20x0.375

      0.2500-20x0.500

      0.2500-20x0.750

       

      to be

       

      0.375

      0.500

      0.750

       

      I found and modified the following macro to iterate through the configurations on the SW VBA help page BUT it will not actual rename the configuration?

      What am I missing?

       

      '--------------------------------------

      ' Preconditions:

      ' 1. Open a part or assembly.

      ' 2. Open the Immediate window.

      '

      ' Postconditions:

      ' 1. Iterates through all configurations.

      ' 2. Examine the Immediate window.

      '---------------------------------------------------------

      Option Explicit

      Sub main()

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim vConfNameArr As Variant

          Dim sConfigName As String

          Dim nStart As Single

          Dim i As Long

          Dim bShowConfig As Boolean

          Dim bRebuild As Boolean

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Debug.Print "File = " + swModel.GetPathName

          vConfNameArr = swModel.GetConfigurationNames

          For i = 0 To UBound(vConfNameArr)

              sConfigName = vConfNameArr(i)

              Debug.Print "  Old Configuration Name = " & sConfigName

              sConfigName = Right(sConfigName, Len(sConfigName) - 10)

              bShowConfig = swModel.ShowConfiguration2(sConfigName)

              Debug.Print "  New Configuration Name= " & sConfigName

              bRebuild = swModel.ForceRebuild3(False)

          Next i

      End Sub

        • Re: How to batch rename configurations?
          Deepak Gupta

          Try these codes

           

          Option Explicit

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim vConfNameArr As Variant

              Dim sConfigName As String

              Dim i As Long

          Sub main()

           

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

              vConfNameArr = swModel.GetConfigurationNames

              For i = 0 To UBound(vConfNameArr)

                   sConfigName = vConfNameArr(i)

                   swModel.ShowConfiguration2 sConfigName

                   swApp.ActiveDoc.ConfigurationManager.ActiveConfiguration.Name = Right(sConfigName, Len(sConfigName) - 10)

              Next i   

          swModel.ForceRebuild3 False

          End Sub

          • Re: How to batch rename configurations?
            Keith Rice
            Sub main()
                Dim swApp As SldWorks.SldWorks
                Dim swModel As ModelDoc2
                Dim swConfig As Configuration
                Dim vConfigNames As Variant
                Dim i As Integer
                
                Set swApp = Application.SldWorks
                Set swModel = swApp.ActiveDoc
                vConfigNames = swModel.GetConfigurationNames
                For i = 0 To UBound(vConfigNames)
                    Set swConfig = swModel.GetConfigurationByName(vConfigNames(i))
                    swConfig.Name = Split(swConfig.Name, "x")(1)
                Next i
            End Sub
            

             

            The one you're currently using needs to activate every configuration, which will massively increase the running time of the macro compared to this one.

             

            If you wanted to make the macro more robust you could check to make sure that Split() returns an array of the proper size.

             

            Keith

            SolidWorks API Training and Services