6 Replies Latest reply on Feb 6, 2015 12:12 PM by Xiaodong Zhang

    Run time error 91

    Xiaodong Zhang

      Hi,

      I am pretty new to Solidworks API. I recorded a macro program using macro tools while I was creating an extruded boss. I used smart dimensions in the sketch steps. When I run the API program, it gave me the run-time error 91. Can any one help me solve this problem? Any suggestion is appreciated. Here is the automatically created code in macro:

       

      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

       

       

      boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", -8.52419389497072E-03, 0.12320010397201, 9.98222865168827E-02, False, 0, Nothing, 0)

      Part.SketchManager.InsertSketch True

      Part.ClearSelection2 True

      Dim vSkLines As Variant

      vSkLines = Part.SketchManager.CreateCenterRectangle(2.30630980853676E-02, 0.01630633106817, 0, 3.11712185060046E-02, 8.37839110132493E-03, 0)

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Point1", "SKETCHPOINT", 2.30630980853676E-02, 0.01630633106817, 0, False, 0, Nothing, 0)

      boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)

      Dim myDisplayDim As Object

      Set myDisplayDim = Part.AddDimension2(2.66667071612062E-02, -1.19820001771636E-02, 0)

      Part.ClearSelection2 True

      Dim myDimension As Object

      Set myDimension = Part.Parameter("D1@Sketch1")

      myDimension.SystemValue = 0.01802775637732

      boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 1.54101314312007E-02, 1.53526309407857E-02, 0, False, 0, Nothing, 0)

      Set myDisplayDim = Part.AddDimension2(1.46051245653917E-02, 1.96076672314904E-02, 0)

      boolstatus = Part.Extension.SelectByID2("D1@Sketch1@Part11.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Set myDimension = Part.Parameter("D2@Sketch1")

      myDimension.SystemValue = 0.01

      boolstatus = Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 2.02401726260546E-02, 1.19026015158901E-02, 0, False, 0, Nothing, 0)

      Set myDisplayDim = Part.AddDimension2(2.57602197058877E-02, 1.07525917075915E-02, 0)

      boolstatus = Part.Extension.SelectByID2("D2@Sketch1@Part11.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)

      Part.ClearSelection2 True

      Set myDimension = Part.Parameter("D3@Sketch1")

      myDimension.SystemValue = 0.01

      Part.ClearSelection2 True

      Part.SketchManager.InsertSketch True

      Part.ShowNamedView2 "*Trimetric", 8

      Part.SketchManager.InsertSketch True

      Part.ClearSelection2 True

      boolstatus = Part.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)

      Dim myFeature As Object

      Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 6, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)

      Part.SelectionManager.EnableContourSelection = False

      End Sub

        • Re: Run time error 91
          Keith Rice

          Xiaodong,

           

          You need to have a part open when you run that code, otherwise you will get error 91. With a part open, I didn't get that error.

           

          If you plan on programmatically creating dimensions, you'll want to turn off this option under Tools-->Options:

           

           

          Otherwise, the user will have to manually hit enter every time the "Modify Dimension" dialog box appears. You can turn this option off programmatically using this line of code:

           

          swApp.SetUserPreferenceToggle swInputDimValOnCreate, False

           

          You can turn it back on after you add the dimension using this line:

           

          swApp.SetUserPreferenceToggle swInputDimValOnCreate, True

           

          As for some general suggestions, however, I would recommend two items at my web site:

           

          1. A blog post I wrote called 7 Mistakes New SolidWorks API Programmers Make -- talks about the pitfalls of using the macro recorder.

           

          2. A free PDF (registration required) that covers the most common run-time and compile time errors you'll encounter while using the API with VBA.

           

          Keith

          SolidWorks API Tutorials

            • Re: Run time error 91
              Xiaodong Zhang

              Hello Keith,

              Thank you so much. The problem is still here. When I hit the debug button, it highlighted the following sentence:

               

              myDimension.SystemValue = 0.01802775637732

               

              What should I do to cancel this error?

              Thank you again for valuable help.

                • Re: Run time error 91
                  Ivana Kolin

                  You can run this macro only one time in each new part. You have to use sketch name of just new created sketch.

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim Part As SldWorks.ModelDoc2
                  Dim boolstatus As Boolean
                  Dim longstatus As Long, longwarnings As Long
                  Sub main()
                  Set swApp = Application.SldWorks
                  Set Part = swApp.ActiveDoc
                  swApp.SetUserPreferenceToggle swInputDimValOnCreate, False
                  Dim swSketch As SldWorks.Sketch
                  boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
                  Part.SketchManager.InsertSketch (True)
                  Set swSketch = Part.SketchManager.ActiveSketch
                  Part.ClearSelection2 True
                  Dim vSkLines As Variant
                  vSkLines = Part.SketchManager.CreateCenterRectangle(2.30630980853676E-02, 0.01630633106817, 0, 3.11712185060046E-02, 8.37839110132493E-03, 0)
                  Part.ClearSelection2 True
                  boolstatus = Part.Extension.SelectByID2("Point1", "SKETCHPOINT", 2.30630980853676E-02, 0.01630633106817, 0, False, 0, Nothing, 0)
                  boolstatus = Part.Extension.SelectByID2("Point1@Origin", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
                  Dim myDisplayDim As SldWorks.DisplayDimension
                  Set myDisplayDim = Part.AddDimension2(2.66667071612062E-02, -1.19820001771636E-02, 0)
                  Part.ClearSelection2 True
                  Dim myDimension As SldWorks.Dimension
                  Set myDimension = Part.Parameter("D1@" & swSketch.Name & "")
                  myDimension.SystemValue = 0.01802775637732
                  boolstatus = Part.Extension.SelectByID2("Line1", "SKETCHSEGMENT", 1.54101314312007E-02, 1.53526309407857E-02, 0, False, 0, Nothing, 0)
                  Set myDisplayDim = Part.AddDimension2(1.46051245653917E-02, 1.96076672314904E-02, 0)
                  boolstatus = Part.Extension.SelectByID2("D1@" & swSketch.Name & "@Part11.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
                  Part.ClearSelection2 True
                  Set myDimension = Part.Parameter("D2@" & swSketch.Name & "")
                  myDimension.SystemValue = 0.01
                  boolstatus = Part.Extension.SelectByID2("Line4", "SKETCHSEGMENT", 2.02401726260546E-02, 1.19026015158901E-02, 0, False, 0, Nothing, 0)
                  Set myDisplayDim = Part.AddDimension2(2.57602197058877E-02, 1.07525917075915E-02, 0)
                  boolstatus = Part.Extension.SelectByID2("D2@" & swSketch.Name & "@Part11.SLDPRT", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0)
                  Part.ClearSelection2 True
                  Set myDimension = Part.Parameter("D3@" & swSketch.Name & "")
                  myDimension.SystemValue = 0.01
                  Part.ClearSelection2 True
                  Part.SketchManager.InsertSketch True
                  Part.ShowNamedView2 "*Trimetric", 8
                  Part.SketchManager.InsertSketch True
                  Part.ClearSelection2 True
                  boolstatus = Part.Extension.SelectByID2("" & swSketch.Name & "", "SKETCH", 0, 0, 0, False, 4, Nothing, 0)
                  Dim myFeature As Object
                  Set myFeature = Part.FeatureManager.FeatureExtrusion2(True, False, False, 6, 0, 0.01, 0.01, False, False, False, False, 1.74532925199433E-02, 1.74532925199433E-02, False, False, False, False, True, True, True, 0, 0, False)
                  Part.SelectionManager.EnableContourSelection = False
                  swApp.SetUserPreferenceToggle swInputDimValOnCreate, True
                  End Sub