SOLIDWORKS Macro Automation: It’s About Time

Ask any designer what the most troublesome aspect of their project plan is and you’ll most assuredly hear about time constraints. There’s time required for one-off designs. Time required for change documentation. Time required for the myriad mundane tasks that we all must fit into the schedule. So what about time for the real design work? You know… the stuff we learned in school. The stuff that was so cool that we decided to make it our career. How do we get more of that time? Well, you could ask for an assistant (good luck with that) or you could automate those tedious and time-consuming tasks using SOLIDWORKS Macros.

Macros are small computer programs that you can create right inside of SOLIDWORKS. They are saved out as a separate file and therefore can be used over and over again with any of your models. They are real computer programs, so the more programming skills you have or can acquire, the more time you and your team will be able to save.

But enough talk, let’s take a look under the hood and step through an example of creating a simple macro. Suppose you have a part with two configurations (“abc” and “123”) and you need to start a brand new file from each of the configurations.

Here’s how to create a macro to accomplish that task with the push of a button:

Step 1: Record a Macro

1. Start Recording

  • Select Tools → Macro → Record
  • SOLIDWORKS is now watching your every move and turning each step into a line of code in your program


2. Activate a Configuration

  • RMB → Show Configuration
  • SOLIDWORKS just wrote the following line of code to your macro: Part.ShowConfiguration2(“123”)

3. Save the File

  • File → Save As
  • Type in the configuration name as the new filename
  • SOLIDWORKS just wrote this line of code to your macro: Part.SaveAs3(“C:\temp\123.SLDPRT”, 0, 2)

4. Stop Recording

  • Tools → Macro → Stop
  • Tell SOLIDWORKS to stop watching

5. Save the Macro

  • You will be prompted as soon as you stop recording
  • Save it as a SW VBA Macro (*.swp) file

Congratulations! You have just created your first computer program. To run it just pick Tools → Macro → Run… and browse to your swp file. That’s it!  It just ran again. It should quickly become apparent though that this macro is going to do EXACTLY what you did (and that’s it). It’s going to save the “123” configuration to c:\temp\123.sldprt. That’s pretty cool but our goal is to save all the configurations.

So to view (and improve) the source code, just select Tools → Macro → Edit… and browse to your swp file. This will bring up the source code editor for your macro. Most of the code you see is boiler-plate code that every macro needs. You should be able to spot the 3 lines where the real work is done. These lines select the configuration, then activate it, and finally save the new file. You can simply copy that code and tweak it to add the other configuration into the mix. Here’s how:

  • Copy the three lines to the clipboard and paste back in just below the originals
  • In the new lines change all the “123“s to “abc“s
  • Save and close the macro
  • Run it again (Tools → Macro → Run…)

Viola! Both configurations are now being saved.

But what if more configurations are added to the model later? Do we have to modify the source code every time? Well, you could but there are other ways to make the macro more versatile (i.e. reusable). This is where your programming knowledge comes into play. In this case we can use a SOLIDWORKS API call to have your macro ask the active model (on-the-fly) for a list of each of the configuration names. Then using a “for loop” construct the macro can go through the list and run your 3 lines of code for each name in the list.

It looks something like this:


You will notice that the original “123” was replaced with configNames(i). This is where the macro really takes off. The same lines of code that SOLIDWORKS generated are still running but for the configuration names the macro is dynamically substituting the names supplied by the SOLIDWORKS API.

While this is a fairly handy utility, there is still functionality to be added. For example, we would likely want to strip out the extraneous configurations from the new files. Or prompt the user to select a subset of configurations to export. Maybe add a custom toolbar icon to launch the macro. All these things and more are definitely attainable by leveraging the built-in SOLIDWORKS macro technology.

Even with this simple macro there is still a bit more work to do with this macro before unleashing it into the wild. For example error handling – what happens if we don’t have permission to save the file? Or optimizing – are there faster API calls or programming algorithms to accomplish this task? You might also consider adding comments, logging, testing, source control, etc. These are topics for a future post.

Once you have your macro all dialed in, you are ready to share. To do this you simply give a copy of the file to your coworkers. That’s all. Think about it… they don’t know anything about macros and yet you have just multiplied the time savings by every user to whom you give a copy.

That’s the basics of a SOLIDWORKS Macro. Your task now (in your free time) is to identify and document the repetitious, tedious, or error prone work that you grind through every single day on your projects. This will give you a good list of where your time is going. But wait! Before you start recording macros, hold that list up against some of the newer features of out-of-the-box SOLIDWORKS and contact the Alignex support team. You’ll be surprised how far you can get with standard features and a second set of eyes. If, after that, you’re still left with a need for automation, SOLIDWORKS Macros are definitely something you should consider.

A SOLIDWORKS Macro reference guide is available online at .

Rick Steinberger is an Information Systems & Development Manager at Alignex, Inc. Rick spends his day developing software to enhance and improve the quality of customer data and processes. When he’s not hard at work, he enjoys spending his free time hacking Raspberry Pi (a credit card-size computer) and playing guitar. Find more tech tips and blog posts by Rick Steinberger on the Alignex Blog.

Alignex, Inc.
Alignex, Inc. is the premier provider of SOLIDWORKS software and partner products to the mechanical engineering industry in Minnesota, Wisconsin, Iowa, North Dakota, South Dakota, Colorado and Wyoming. With more than 25 years of technical experience, Alignex offers consulting services, training and support for SOLIDWORKS as well as support for partner products. For more information, visit
Alignex, Inc.