SOLIDWORKS VBA and VSTA Macro Overview
This resource is managed by Code Stack and dedicated to providing easy-to-follow beginner friendly guides for automation of your SOLIDWORKS routine with VBA and VSTA macros.
SOLIDWORKS macro is the most popular way for complimenting SOLIDWORKS functionality via SOLIDWORKS API.
SOLIDWORKS enables macro creation in 3 popular programming languages:
- VBA - the most common language for writing macros. This language is based on popular Visual Basic. Macro is saved in *.swp or *.swb format and it is both executable and source code.
- C# and VB.NET - powered by Microsoft’s Visual Studio for Applications (VSTA) technology. This option enables writing the powerful object-oriented macros in the .NET technology. Macro project is saved as *.csproj or *.vbproj projects. Macro executable is a compiled *.dll with referenced SOLIDWORKS interops.
For more information visit Macro Types page.
Visual Basic for Applications
Visual Basic for Applications (VBA) is the most popular language for writing SOLIDWORKS macros. It has a similar syntax to the macro in other applications, such as MS Word, Excel or other CAD Systems. Current version of VBA supported in macros is 7.0.
Follow the Visual Basic link for detailed tutorials and code examples.
Recording Macros
Macro recording is a simplest way to start exploring of SOLIDWORKS macro functions. Majority of commands executed by SOLIDWORKS can be recorded as a macro command. This enables the possibility to explore SOLIDWORKS API calls and parameters.
Both VBA and VSTA macro can be recorded. Follow Recording Macros article for a detailed guide of recording the macros.
Troubleshooting
Although macro is a software which is written using programming language, it is usually managed by engineers and administrators with beginner level of programming. So troubleshooting the macro to fix the error can be a frustrating routine.
Follow the Macros Troubleshooting: Issues And Resolutions section for the list of solutions for the most common macro errors, such as:
- Run-time Error ‘91’: Object variable or With block variable not set
- Run-time error ‘424’: Object required
- Run-time error ‘13’: Type mismatch
- Inconsistent Selections in SOLIDWORKS document
- SOLIDWORKS file title and extension
and much more…
Macro Library
There are thousands of useful macros already available for download on various resources, such as SOLIDWORKS Forum, 3D Content Central, etc. Code Stack.
Please see below several featured macros. Visit Macro Library page for more macros.
Remove All Colors From Part
Colors can be assigned to multiple entities in the SOLIDWORKS document (component, face, feature, body). And it might be challenging to remove the color from the part. This macro removes all the colors from all levels in SOLIDWORKS part document
Copy Custom Property From Material To Model
Custom materials is a good way to support additional materials with additional properties for your SOLIDWORKS file. Unfortunately custom materials properties cannot be queried from the document BOM tables or annotations. This macro allows copying the material custom properties into SOLIDWORKS custom properties
Suspend Rebuild Operation
This macro allows to enhance SOLIDWORKS performance by temporarily disabling rebuild operations. This is very useful when sequence of operations needs to be performed in the large model and regeneration of each operations decreases the performance
Break All External References For Components
Macro breaks all external references for the components in the assembly with single click. This allow to increase performance by disabling external links resolution
Move To Folder
This macro provides a single click function to move components selected in the graphics area in new feature folder. This enhances the built-in functionality of SOLIDWORKS allowing the selection of entities in the model view (not only in the feature manager tree)
Copy File Specific Custom Properties To Configuration Properties
Configuration specific property is a way to override the metadata assigned to a part on configuration level. This macro copies the properties and values from file level to configuration level
Animate Configurations
This macro create a motion study animation between the configurations. This macro can be used to animate the sheet metal bending in SOLIDWORKS part
Close All Documents Except Active
This macro provides a single click way to close all documents except of the active one