6 Replies Latest reply on Jun 28, 2017 8:50 AM by Mark Evans

    Needing help with VBA macro

    Mark Evans

      Hello,

      I have a solidworks part (a tube) which has 24 different length configurations. I am needing to save a parasolid file and a step file of each configuration to a folder of my designation. I wrote a macro that will do this for the active configuration, but when I manually switch to the next configuration and run the macro again, it will save the parasolid/step files with the previously defined file names.

       

      Can someone give me some insight in how to correct this? Ideally, I'd like the macro to save the new parasolid/step files using the name of the specific active configuration with a -001 extension. Or...being able to manually input a name would be fine also. I'm looking to replace 1273262 in the text below with the active configuration name and then add on the -001 suffix.

       

      I'm not a VBA user at all...but by macro code that I have developed so far is shown below.

       

      ' ******************************************************************************
      ' C:\Users\maevans\AppData\Local\Temp\swx12672\Macro1.swb - macro recorded on 06/27/17 by maevans
      ' ******************************************************************************
      Dim swApp As Object

      Dim Part As Object
      Dim boolstatus As Boolean
      Dim longstatus As Long, longwarnings As Long

      Sub main()

      Set swApp = Application.SldWorks

      Set Part = swApp.ActiveDoc
      Dim myModelView As Object
      Set myModelView = Part.ActiveView
      myModelView.FrameState = swWindowState_e.swWindowMaximized

      ' Save As
      longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-001.x_t", 0, 0)

      ' Save As
      longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-001.STEP", 0, 0)
      End Sub

        • Re: Needing help with VBA macro
          Peter Kennedy

          Could you not have the macro cycle through all the configurations for your? Use GetConfigurationNames, have the macro loop through each name. To have it change the name to save you need to change these lines:

           

          ' Save As
          longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-001.x_t", 0, 0)

          ' Save As
          longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-001.STEP", 0, 0)

           

          to something like

           

          Configuration_Name = CurrentConfirguration

           

          ' Save As
          longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-" & Configuration_Name & ".x_t", 0, 0)

          ' Save As
          longstatus = Part.SaveAs3("C:\Users\maevans\Desktop\Tubes-CAD\1273262-" & Configuration_Name & ".STEP", 0, 0)

          • Re: Needing help with VBA macro
            Raghvendra Bhargava

            Hi Mark,

            Try this:

            Dim swApp As SldWorks.SldWorks

            Dim Part As ModelDoc2

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long

            Dim configNameArr As Variant

            Dim cnt As Integer

             

            Sub main()

             

            Set swApp = Application.SldWorks

            Set Part = swApp.ActiveDoc

             

            configNameArr = Part.GetConfigurationNames

             

            For cnt = 0 To UBound(configNameArr)

                boolstatus = Part.Extension.SaveAs("C:\Users\maevans\Desktop\Tubes-CAD\" & configNameArr(cnt) & "-001" & ".X_T", 0, 1, Nothing, longstatus, longwarnings)

                boolstatus = Part.Extension.SaveAs("C:\Users\maevans\Desktop\Tubes-CAD\" & configNameArr(cnt) & "-001" & ".step", 0, 1, Nothing, longstatus, longwarnings)

                If cnt < UBound(configNameArr) Then

                    Part.ShowConfiguration2 configNameArr(cnt + 1)

                    Set Part = swApp.ActiveDoc

                End If

            Next cnt

            End Sub

             

            if it won't work try to change the path from C drive .