Made in Boston

My Introduction to SolidWorks Macros


Recently at work, I’ve been creating a lot of simple parts from 2D drawings.  And when I say simple, this is what I mean:


Each of these parts was given a part number and description using Custom Properties:


But then, when I wanted to save the part, I had to type the part number and description in again.


I realized that adding the custom properties, and then typing them in again to save them was taking longer than modeling the actual parts.  So, I asked the SolidWorks Forums if anyone knew of a faster way to save a part automatically using custom properties.

The infamous Deepak Gupta quickly replied with a macro script that did exactly what I needed. 


To create a macro, first show the macro toolbar my right-clicking anywhere in the CommandManager:


Then, click the New Macro button, which asks you to name your new macro, then launches Microsoft Visual Basic:


Write or paste the macro code into the VBA window.  This is the code for saving a part automatically in the format PartNo – Description.sldprt:

   Sub main()

   Dim swApp As SldWorks.SldWorks

   Dim swModel As SldWorks.ModelDoc2

   Dim PartNo As String, Description As String


   Set swApp = Application.SldWorks

   Set swModel = swApp.ActiveDoc


   PartNo = swModel.CustomInfo(“PartNo”)

   Description = swModel.CustomInfo(“Description”)


   swModel.SaveAs (PartNo + ” – ” + Description + “.sldprt”)


   End Sub



Even if you know nothing about macros or VBA, you can probably get the gist of it. The macro focuses on the current open document, grabs the ‘PartNo’ and ‘Description’ properties, and assigns them to spaces in the Save As field.  It also adds a dash, and a .sldprt to the filename.


Save your macro and close VBA.  Then, when you’re ready to save your part (after filling out the PartNo and Description fields in custom properties) just click the Run Macro button, and choose the macro you just created.



You’ll notice that the file name at the very top of the SolidWorks window changes from Part1* to the correct PartNo – Description.sldprt filename.  (For my example, this is SK80005 – Plate, Front, Cradle.sldprt)


Now go show your boss how efficient you are and get that raise!

Tags: , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Downloadable SolidWorks Models


%d bloggers like this: