How to Write SolidWorks Macro in VBA: Simple Guide
To write a SolidWorks macro in
VBA, open the SolidWorks VBA editor, create a new macro, and use the ISldWorks interface to control SolidWorks objects. Write your code inside the Sub main() procedure to automate tasks like opening documents or creating features.Syntax
A SolidWorks VBA macro typically starts with a Sub main() procedure where you write your automation code. You use the ISldWorks object to interact with SolidWorks, and ModelDoc2 to work with documents. Common methods include OpenDoc6 to open files and CreateFeature to add features.
Example parts:
Dim swApp As SldWorks.ISldWorks: Declares the SolidWorks application object.Set swApp = Application.SldWorks: Connects to the running SolidWorks instance.Dim swModel As SldWorks.ModelDoc2: Declares the active document.Set swModel = swApp.ActiveDoc: Gets the current open document.
vba
Sub main()
Dim swApp As SldWorks.ISldWorks
Dim swModel As SldWorks.ModelDoc2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' Your automation code here
End SubExample
This example macro opens a SolidWorks part file and shows a message box with the document title. It demonstrates connecting to SolidWorks, opening a document, and accessing its properties.
vba
Sub main()
Dim swApp As SldWorks.ISldWorks
Dim swModel As SldWorks.ModelDoc2
Dim filePath As String
Dim errors As Long
Dim warnings As Long
Set swApp = Application.SldWorks
filePath = "C:\\Users\\Public\\Documents\\SamplePart.sldprt"
Set swModel = swApp.OpenDoc6(filePath, swDocPART, 0, "", errors, warnings)
If Not swModel Is Nothing Then
MsgBox "Opened document: " & swModel.GetTitle()
Else
MsgBox "Failed to open document."
End If
End SubOutput
A message box appears with text: "Opened document: SamplePart.sldprt" if successful.
Common Pitfalls
Common mistakes when writing SolidWorks VBA macros include:
- Not setting the
swAppobject properly, causing errors when calling SolidWorks methods. - Using incorrect document type constants (e.g.,
swDocPART,swDocASSEMBLY). - Forgetting to check if the document opened successfully before accessing its properties.
- Not handling errors, which can cause the macro to crash silently.
Always verify objects are set and use error handling to make your macro robust.
vba
'' Wrong way: Not setting swApp Sub main() Dim swModel As SldWorks.ModelDoc2 Set swModel = swApp.ActiveDoc ' swApp not set, causes error End Sub '' Right way: Sub main() Dim swApp As SldWorks.ISldWorks Dim swModel As SldWorks.ModelDoc2 Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc End Sub
Quick Reference
| Concept | Description | Example |
|---|---|---|
| swApp | SolidWorks application object | Set swApp = Application.SldWorks |
| swModel | Active document object | Set swModel = swApp.ActiveDoc |
| OpenDoc6 | Open a SolidWorks file | Set swModel = swApp.OpenDoc6(filePath, swDocPART, 0, "", errors, warnings) |
| GetTitle | Get document title | swModel.GetTitle() |
| swDocPART | Constant for part document | Used as second argument in OpenDoc6 |
Key Takeaways
Always start your macro with Sub main() and connect to SolidWorks using swApp.
Use swApp.OpenDoc6 to open files and swModel to interact with documents.
Check objects are set before using them to avoid runtime errors.
Use proper document type constants like swDocPART for parts.
Add error handling to make your macro stable and user-friendly.