5 Simple SolidWorks Macros Every Design Engineer Should Know
Copy-paste ready VBA macros that save time immediately. No coding experience required.
Copy these, run them today. No programming degree required.
By Prasath | ScriptedCAD | https://scriptedcad.com
Every design engineer I know has spent hours doing the same thing in SolidWorks over and over. Export this drawing. Update that title block. Rebuild and save before the release. Again. And again.
Here is the part nobody told you: SolidWorks has a built-in macro recorder and a full API underneath. You can automate almost any repetitive action. Some macros need no code at all. The ones that do need code are 10 to 20 lines of VBA you can copy, paste, and run in under 5 minutes.
These are the 5 macros I wish someone had handed me on day one.
Macro 1: Record and Replay Any Action
This is the entry point. No code. No API knowledge. Just record what you do and play it back.
How to do it:
- Go to Tools > Macro > Record
- Perform any action in SolidWorks (apply a view, insert a note, change a display state)
- Click Stop and save the file as a .swp macro
- Run it again with Tools > Macro > Run
Real scenario: You apply the same isometric view with shaded-with-edges display every time you send a screenshot to a customer. That is 6 clicks every time. Record it once. Now it is 1 click.
You can assign macros to a toolbar button or keyboard shortcut in Tools > Customize > Toolbars. That turns any recorded sequence into a single key press.
Macro 2: Save the Active Drawing as PDF
Ask any design engineer what they do 10 to 20 times a day. Exporting drawings to PDF is near the top. This macro saves the active drawing as a PDF in the same folder. No dialog boxes. No clicking through the save menu.
Sub SaveAsPDF()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sFilePath As String
Dim errors As Long
Dim warnings As Long
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "No active document found."
Exit Sub
End If
sFilePath = Left(swModel.GetPathName, Len(swModel.GetPathName) - 7) & ".pdf"
swModel.Extension.SaveAs sFilePath, 0, 0, Nothing, errors, warnings
MsgBox "Saved: " & sFilePath
End SubTo use this: go to Tools > Macro > New, paste the code, save it. Then assign it to a keyboard shortcut in Tools > Customize. One key press and the PDF is done.
Macro 3: Update Custom Properties on a Part
Title blocks pull their data from custom properties: part number, description, revision, material. If you are filling these in manually on every file you open, you are losing 5 to 10 minutes per part.
This macro sets the three most common properties on the active document in one run. Change the values in the code to match your part before running.
Sub UpdateCustomProperties()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
swCustPropMgr.Add3 "PartNo", swCustomInfoText, "PN-001", swCustomPropertyReplaceValue
swCustPropMgr.Add3 "Description", swCustomInfoText, "Bracket Assembly", swCustomPropertyReplaceValue
swCustPropMgr.Add3 "Revision", swCustomInfoText, "A", swCustomPropertyReplaceValue
MsgBox "Custom properties updated."
End SubMacro 4: Batch Export All Drawings in a Folder to PDF
The single-file PDF macro is useful. But what happens when you need to export 50 drawings before a review meeting that starts in 30 minutes?
This macro opens every .SLDDRW file in a folder you specify, exports it as PDF, and closes it. What used to take 30 to 40 minutes of clicking now runs in the background while you prepare your slides.
Sub BatchExportToPDF()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim sFolder As String
Dim sFile As String
Dim sPDFPath As String
Dim errors As Long
Dim warnings As Long
Set swApp = Application.SldWorks
sFolder = "C:\Drawings\" ' Change this to your drawings folder path
sFile = Dir(sFolder & "*.SLDDRW")
Do While sFile <> ""
Set swModel = swApp.OpenDoc6(sFolder & sFile, swDocDRAWING, swOpenDocOptions_Silent, "", errors, warnings)
sPDFPath = sFolder & Left(sFile, Len(sFile) - 7) & ".pdf"
swModel.Extension.SaveAs sPDFPath, 0, 0, Nothing, errors, warnings
swApp.CloseDoc swModel.GetPathName
sFile = Dir
Loop
MsgBox "Batch export complete."
End SubMacro 5: Rebuild All Open Documents and Save
Before releasing any files, every open document should be force-rebuilt and saved. Most engineers skip this or forget. Stale models cause problems downstream: wrong dimensions in drawings, incorrect BOMs, failed exports, errors in assemblies.
This macro loops through every open document in SolidWorks, forces a full rebuild, and saves it.
Sub RebuildAndSaveAll()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim errors As Long
Dim warnings As Long
Set swApp = Application.SldWorks
Set swModel = swApp.GetFirstDocument
Do While Not swModel Is Nothing
swModel.ForceRebuild3 False
swModel.Save3 swSaveAsCurrentVersion, errors, warnings
Set swModel = swModel.GetNext
Loop
MsgBox "All documents rebuilt and saved."
End SubWhat These 5 Macros Actually Prove
Task automation is the entry level. Repetitive actions become one click. Hours become minutes. That is real value and you can start delivering it today.
But the same API that powers these macros also powers things at a completely different scale:
- Product configurators that generate complete models and drawing packages from customer inputs
- Automated quoting systems that replace 3 days of manual work with a 20-minute process
- Drawing QA checkers that audit every sheet before release and flag errors automatically
- Design validation tools that check manufacturing constraints before the file ever leaves CAD
The engineers who build those tools are not senior developers. They are design engineers who started exactly where you are right now: with a simple macro they copied and ran for the first time.
What To Do Next
Pick one macro from this guide. Open SolidWorks. Go to Tools > Macro > New. Paste the code. Run it on a test file.
That first run is the moment everything changes. You will see SolidWorks do something you used to do by hand, and you will understand why engineers who learn this never go back.