createSketch2.bmp
createSketch2.swp
  1.  
  2. 'precondition: Adjust line 11 below to point to the default part document of your version of SolidWorks
  3.  
  4. Dim swApp As Object
  5. Dim part As Object
  6. Dim boolstatus As Boolean
  7.  
  8. Sub main()
  9.  
  10. Set swApp = Application.SldWorks
  11. Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2017\templates\Part.prtdot", 0, 0, 0) 'This line opens up SolidWorks default part document. If you dont have SW 2017, or you've installed SolidWorks to a different location, this line will need to be changed.
  12. 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\Part.prtdot", 0, 0, 0)
  13. 'Set part = swApp.NewDocument("C:\ProgramData\SolidWorks\SolidWorks 2013\templates\Part.prtdot", 0, 0, 0)
  14. Set part = swApp.ActiveDoc
  15. boolstatus = part.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
  16. part.SketchManager.InsertSketch True
  17. part.SetPickMode
  18. part.ClearSelection2 True
  19.  
  20. End Sub
  21.  

    This macro will create a new part document, select the Top Plane, then insert a sketch. You'll have to point this macro to the default part document file. If you are using Solidworks 2017 you may not have to adjust the code at all. But if you have any other version of Solidworks, or you have Solidworks installed in a different directory than I do, you'll need to change line 11 of the macro code. If you want this macro to create a sketch on the Front or Right plane simply change line 15 in the code. Change the word Top in "Top Plane" to Front or Right.

    See also Create Sketch for a macro which requires you to have a part document open already. For more simple macro examples check out the DeleteRelations and DeleteRelations2 macros. They demonstrate how to remove sketch constraints from inside and outside a sketch respectively. Return to the SolidWorks VBA Macros page.