3 Replies Latest reply on Apr 2, 2015 3:31 PM by Keith Rice

    Why is my NewPart failing?

    Daniel Nesthus

      I have tried a few things and I'm now stuck.  Here is a snippet:

       

      Dim SwModel As ModelDoc2

       

      Set SwModel= swApp.NewPart(sFilename, 0, 0, 0)

       

      This sequence will manage to properly open a new part based on the part template sFilename, but then I get a message that the "Object doesn't support this property or method" on that line.  I thought it was perhaps because it was a newly created item and I found some example code that did the following instead:

       

      Dim swNewPart As PartDoc

       

      Set swNewPart = swApp.NewPart(sFilename, 0, 0, 0)

      SwModel = swNewPart

       

      Again, it will open a new part but I get the same message on the 'set' line.  It definitely evaluates the right side, but seems to hang up on the left assignment.

       

      Am I using an incorrect object (not PartDoc or ModelDoc2)?  Is there another?

        • Re: Why is my NewPart failing?
          Keith Rice

          The reason you're getting that error is that ISldWorks::NewPart doesn't have any arguments. Incidentally those are the arguments that should be provided when creating a new part with ISldWorks::NewDocument, which is the correct way to create a new document.

           

          Please change TEMPLATE_PATH below to use your actual part template.

           

          Const TEMPLATE_PATH As String = _
              "C:\ProgramData\SolidWorks\SolidWorks 2011\templates\Part MM.prtdot"
          
          Dim swApp As SldWorks.SldWorks
          Dim swModel As SldWorks.ModelDoc2
          
          Sub main()
              Set swApp = Application.SldWorks
              Set swModel = swApp.NewDocument(TEMPLATE_PATH, Empty, Empty, Empty)
          End Sub
          

           

          Resolving problems like the one above require a knowledge of how to use the API Help. I would also encourage you to check out this article: 7 Mistakes New SolidWorks API Programmers Make

           

          Keith

          SolidWorks API Tutorials

            • Re: Why is my NewPart failing?
              Daniel Nesthus

              Keith,

               

              Ah, 'NewDocument' not 'NewPart'.  That seems to have worked completely.

               

              I have read through the information you have at CAD-Sharp and other areas and I have been writing VBA for Office applications for over 10 years, but the objects and methods for Solidworks are throwing me around a bit!

               

              What was strange was that the 'NewPart' method almost works.  It actually creates a new part using the part template called "sFilename".  The error occurs sometime after the instantiation of the model document but before the assignment to swModel.