- solidworks
- vba
- macro
- automation
SolidWorks Macro Tutorial: Your First VBA Macro
Vikraman N4 min read
A SolidWorks macro is a small program that drives SolidWorks for you. Anything you click — new part, sketch, extrude, save — the macro can do through the SolidWorks API, and it will do it the same way every time. This tutorial takes you from recording a macro to writing one that builds a part on its own. You need SolidWorks (any recent version) and nothing else: the VBA editor is built in.
1. Record a macro and read what SolidWorks wrote
- Open a new part. Go to Tools → Macro → Record (or the Record button on the Macro toolbar).
- Sketch a rectangle on the Front plane and extrude it 10 mm.
- Tools → Macro → Stop, save it as
FirstMacro.swp. - Tools → Macro → Edit, pick the file. The VBA editor opens with code like this:
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0)
Part.SketchManager.InsertSketch True
Part.SketchManager.CreateCornerRectangle 0, 0, 0, 0.05, 0.03, 0
Part.FeatureManager.FeatureExtrusion2 True, False, False, 0, 0, 0.01, 0.01, False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False
Part.ClearSelection2 True
End Sub
Three things to notice, because they are the whole SolidWorks API in miniature:
swAppis SolidWorks itself (SldWorks),Partis the open document (ModelDoc2). Every macro starts by getting these two.- Lengths are in metres:
0.05is 50 mm,0.01is 10 mm. Forgetting this is the number-one beginner bug. FeatureExtrusion2has a long list of arguments. The recorder fills them in; you only ever change one or two (depth, direction).
2. Turn the recording into a macro that builds the part itself
The recorded macro assumes a part is already open and a sketch plane is selected by name. Let us make it self-contained: create a new part from the default template, sketch, extrude, and name the feature.
Option Explicit
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeat As SldWorks.Feature
Dim template As String
Set swApp = Application.SldWorks
template = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
Set swModel = swApp.NewDocument(template, 0, 0, 0)
' sketch a 50 x 30 mm rectangle on the Front plane
swModel.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
swModel.SketchManager.InsertSketch True
swModel.SketchManager.CreateCornerRectangle 0, 0, 0, 0.05, 0.03, 0
swModel.SketchManager.InsertSketch True ' close the sketch
' extrude it 10 mm (blind, one direction)
swModel.Extension.SelectByID2 "Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0
Set swFeat = swModel.FeatureManager.FeatureExtrusion2(True, False, False, 0, 0, 0.01, 0.01, _
False, False, False, False, 0, 0, False, False, False, False, True, True, True, 0, 0, False)
If Not swFeat Is Nothing Then
swFeat.Name = "Base-Plate"
End If
swModel.ViewZoomtofit2
End Sub
Before running it, add the type library so VBA knows the SolidWorks types: in the VBA editor, Tools → References, tick SldWorks 20xx Type Library and SolidWorks 20xx Constant type library. That is what makes SldWorks.ModelDoc2 and swUserPreferenceStringValue_e resolve, and it gives you IntelliSense for every method.
Run it with F5. SolidWorks opens a new part, draws the rectangle, extrudes it and renames the feature. Change 0.05, 0.03 and 0.01 and run it again — that is already a parametric part generator.
3. Make it ask for the sizes
A macro that asks for inputs is the first thing engineers actually use day to day. Replace the three numbers with a tiny prompt:
Dim w As Double, h As Double, d As Double
w = CDbl(InputBox("Width (mm)", "Base plate", 50)) / 1000
h = CDbl(InputBox("Height (mm)", "Base plate", 30)) / 1000
d = CDbl(InputBox("Thickness (mm)", "Base plate", 10)) / 1000
and use w, h in CreateCornerRectangle and d for both depth arguments of FeatureExtrusion2. Dividing by 1000 converts the millimetres people type into the metres the API expects.
4. Where to go next
- Traverse what exists:
swModel.FirstFeatureandFeature.GetNextFeaturewalk the feature tree — the basis of every "find all holes and change their diameter" macro. - Drawings and BOMs:
DrawingDoc.CreateDrawViewFromModelView3places views;ModelDoc2.SaveAs3exports PDF/DXF in a batch. - From VBA to C#: the same objects and methods are available from C# as a standalone program or an add-in, which is where you go when a macro grows into a tool other people use.
All of that is the curriculum of the SolidWorks API & Automation Course: it opens with exactly these VBA macros and continues to C# add-ins, drawings and batch processing. If you want to try the teaching style first, the trial lesson is the first chapter of that course.
Common errors, and what they mean
| Message | Cause | Fix |
|---|---|---|
Object variable or With block variable not set | swApp.ActiveDoc was Nothing — no document open | Open a part, or create one with NewDocument as above |
User-defined type not defined | Type library reference missing | Tools → References → tick the SldWorks type libraries |
| Rectangle is tiny or huge | Millimetres given where metres are expected | Divide by 1000 |
FeatureExtrusion2 returns Nothing | The sketch was not selected, or is still open | Close the sketch with InsertSketch True, then select it by name |