3 Replies Latest reply on Nov 29, 2016 5:10 PM by Viktor Bovzdarenko

    "Compile Error: Sub or Function Not Defined"

    Andrew Wallace

      I am trying to write an extremely basic Macro and it is giving me fits. I just want a user form that will allow the user to input the length and diameter of a cylinder and SolidWorks draw and extrude it for them. I literally watched a step by step tutorial and almost copied the code word for word but it is giving me the error "Compile Error: Sub or Function Not Defined". I have absolutely no idea why it isn't working but this is my first time writing a macro code for SolidWorks so it could be something blatantly obvious that I am missing. Any help would be greatly appreciated.


      Here is my code:

      ' ******************************************************************************

      ' C:\Users\awallace\AppData\Local\Temp\swx1764\Macro1.swb - macro recorded on 11/28/16 by awallace

      ' ******************************************************************************

      Dim swApp As Object



      Dim Part As ModelDoc2

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long



      Sub main()


           'Display User Form



           'Opens new part in SolidWorks

           Set swApp = Application.SldWorks


           Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2015\templates\Part.prtdot", 0, 0, 0)

           swApp.ActivateDoc2 "Part2", False, longstatus

           Set Part = swApp.ActiveDoc

           Dim myModelView As Object

           Set myModelView = Part.ActiveView

           myModelView.FrameState = swWindowState_e.swWindowMaximized

           boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

           Part.SketchManager.InsertSketch True


      End Sub


      Public Sub CreateCylinder(InputLength As Double, InputDia As Double)



          'Define Variables

          Dim CylinderLength As Double

          Dim CylinderDia As Double


          'Inputs User Values

          CylinderLength = InputLength

          CylinderDia = InputDia


          'Convert to Meters

          CylinderLength = ConvertToMeters(CylinderLength)

          CylinderDia = ConvertToMeters(CylinderDia)


          'Creating the SketchManager

          Dim swSketchManager As SketchManager

          Set swSketchManager = Part.SketchManager


          'Create Circle

          Dim TheCircle As SketchSegment

          Set TheCircle = swSketchManager.CreateCircle(0, 0, 0, CylinderDia / 2, 0, 0)


          'Creating the SketchManager

          Dim swFeatureManager As FeatureManager

          Set swFeatureManager = Part.FeatureManager


          'Create Circle

          Dim TheLength As Feature

          Set TheLength = swFeatureManager.FeatureExtrusion2(True, False, False, swEndCondBlind, swEndCondBlind, CylinderLength, 0, False, False, False, False, 0, 0, False, False, False, False, False, False, False, False, False, False)


      End Sub


      Here is also the code for the userform:

      Private Sub CommandButton1_Click()


          'Create Variables

          Dim Length As Double

          Dim Dia As Double


          'Set User Inputs Equal to Variables

          Length = LengthTextBox.Text

          Dia = DiaTextBox.Text


          Call CreateCylinder(Length, Dia)




      End Sub

        • Re: "Compile Error: Sub or Function Not Defined"
          Viktor Bovzdarenko

          Hi Andrew

          Looks like ConvertToMeters is not defined anywhere

          instead of

               CylinderLength = ConvertToMeters(CylinderLength)

               CylinderDia = ConvertToMeters(CylinderDia)

          try to use

               CylinderLength = CylinderLength/1000 'Converts from mm to m

               CylinderDia = CylinderDia/1000 'Converts from mm to m

          or define the function ConvertToMeters somewhere:


          Function ConvertToMeters (dblVal As Double) As Double

               ConvertToMeters  = dblVal /1000

          End Function


          Instead of 1000 there should be any other conversion coefficient (1000 is from mm to m, any other could be used to convert from imperial)