SOLIDWORKS API Automation: Developing Custom Engineering Workflows Using VBA and C#
By Panshul Technologies LLP

SOLIDWORKS API Automation: Developing Custom Engineering Workflows Using VBA and C#
Standard SOLIDWORKS features, such as design tables, templates and the Task Scheduler, cover a lot of repetitive work. But sooner or later, most engineering teams hit a task the interface cannot do: pushing metadata into an ERP, renaming thousands of files by rule, generating a model from a customer order, or running a custom check before release.
That is where the SOLIDWORKS API comes in. This guide explains how the API is organized, when to use VBA macros and when to use C# add-ins or standalone applications, how to handle common tasks such as document operations and property extraction, and how to avoid the mistakes that make automation fragile. It is written for CAD developers, automation engineers and CAD administrators.
What Is the SOLIDWORKS API?
The SOLIDWORKS API is a programming interface that lets code control SOLIDWORKS: open and save files, read and write properties, create and edit features, traverse assemblies, generate drawings and export data. It is based on COM, so it can be used from VBA, VB.NET, C# and C++.
Anything you can do by hand in the interface can often be done in code, and some things can be done in code that cannot be done by hand, such as processing hundreds of files unattended. The API is documented in the SOLIDWORKS API Help, which also covers related APIs such as the Document Manager, Simulation, Toolbox, Costing and eDrawings APIs.
If you are only getting started with automation concepts, read our overview of SOLIDWORKS design automation first. This article assumes you are ready to write code.
VBA Macros vs C# Add-ins vs Standalone Applications
.
A simple rule of thumb
• Use VBA to prototype quickly, automate your own repetitive steps, or learn the object model.
• Use C# add-ins when a tool will be shared, needs a user interface, or must be maintained across SOLIDWORKS releases.
• Use a standalone application when the process is driven by something outside SOLIDWORKS, such as an ERP order or a scheduled batch run.
Understanding the SOLIDWORKS API Object Model
Common API Tasks, with Examples
The examples below show the shape of typical code. Test and adapt them for your SOLIDWORKS version and company standards before using them in production.
1. Connecting to SOLIDWORKS and reading the active document
In a VBA macro, the application object is available directly:
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
If swModel Is Nothing Then
MsgBox "Open a document first."
Exit Sub
End If
Debug.Print swModel.GetTitle
End Sub
A standalone C# application must start or attach to SOLIDWORKS itself:
Type swType = Type.GetTypeFromProgID("SldWorks.Application");
SldWorks swApp = (SldWorks)Activator.CreateInstance(swType);
swApp.Visible = true;
2. Opening documents safely
int errors = 0, warnings = 0;
ModelDoc2 model = swApp.OpenDoc6(
filePath,
(int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"", ref errors, ref warnings);
if (model == null) { /* log errors and skip this file */ }
Always check for a null result and log the error codes. In batch work, a single corrupt or missing-reference file should not stop the whole run.
3. Reading and writing custom properties
File-level and configuration-specific properties are accessed through the CustomPropertyManager. Passing an empty string selects file-level properties, and a configuration name selects that configuration's properties.
Dim swCustPropMgr As SldWorks.CustomPropertyManager
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
' Write or replace a property
swCustPropMgr.Add3 "Description", swCustomInfoType_e.swCustomInfoText, _
"Mounting bracket", swCustomPropertyAddOption_e.swCustomPropertyReplaceValue
' Read a property and its resolved value
Dim rawValue As String, resolvedValue As String, wasResolved As Boolean
swCustPropMgr.Get5 "Description", False, rawValue, resolvedValue, wasResolved
This is the basis for tools that populate title blocks, sync metadata with an ERP or enforce property standards.
4. Traversing an assembly
Assembly work usually means walking the component tree, often recursively, to build a bill of materials, check references or collect file names. Remember to handle suppressed components, lightweight components, virtual components and configurations, since each can change what the code sees.
5. Traversing features and creating geometry
The feature tree can be walked with the first-feature and next-feature calls on the model. Creating features is done through the FeatureManager, for example extrusions, cuts, fillets and patterns. These methods tend to have many parameters, and they rely on correctly made sketches and selections. The macro recorder is useful for finding the right method and its argument order.
For design variants, creating geometry from scratch with the API is usually worth the effort only when the geometry follows clear rules, otherwise parametric models with design tables or rules-based tools are easier to maintain.
6. Working with drawings
Drawing automation typically covers inserting views, setting scale, adding annotations from model data, filling title block properties and exporting to PDF or DXF. For sheet metal parts, flat pattern views and DXF exports are common targets, and they connect well with nesting workflows.
7. Batch processing
A typical batch tool follows this pattern:
1. Collect a list of files, from a folder, a PDM vault or a spreadsheet.
2. Open each file silently, and check for errors.
3. Do the work: update properties, rebuild, export, or run checks.
4. Save or close without saving, as appropriate.
5. Write results to a log, including skipped and failed files.
Build in error handling, a progress log and a way to resume after interruption. Test on a copy of real data before running on production files.
8. Integrating with external applications
The API is often one end of a larger workflow:
• Excel: read parameters from a spreadsheet, and write bills of materials or property reports back.
• ERP and databases: read item numbers and write them to properties, or send BOM data to other systems.
• PDM: use the PDM API for vault operations, workflows and data cards. According to a SOLIDWORKS reseller, the PDM API is as capable as the SOLIDWORKS API, but PDM has add-ins and standalone applications rather than macros, so it is written in C#, VB.NET or C++ and not VBA.
• Document Manager API: reads file data such as properties and references without launching SOLIDWORKS, useful for fast reporting. It requires its own licence key from SOLIDWORKS.
• Task Scheduler and command-line triggers: run your tools unattended on a schedule.
Best Practices for Reliable API Automation
Design before you code
Write down the input, the rules, the output and the exceptions. Many automation failures are unclear process problems, not coding problems.
Handle errors and nulls
API calls often return null or error codes when a file is missing, locked, read-only or created in a different version. Check each result.
Keep UI and logic separate
Put business rules in classes you can test, and keep the interface thin. This makes it easier to reuse code between a macro, an add-in and a standalone tool.
Use version control and naming conventions
Store code in source control, tag releases, and document which SOLIDWORKS versions have been tested.
Test on every SOLIDWORKS release
API methods are sometimes superseded by newer versions of the same call. Test before upgrading, and read the API release notes.
Respect licences, vault rules and permissions
Automation must follow the same rules as users, including check-out, revision control and access permissions.
Document and hand over
An undocumented tool becomes a risk when its author leaves. Write short usage notes, input formats and known limitations.
Plan deployment
Add-ins need installation, registration and updates across user machines. Plan distribution, whether by installer, network share or managed deployment.
Common Mistakes
• Shipping recorded macros as finished tools. Recorder code depends on selections and can be brittle.
• Ignoring configurations and references. Code that works on a simple part may fail on assemblies with lightweight or suppressed components.
• No error handling. One bad file can crash a batch run.
• Hard-coded paths and values. These break as soon as folders or standards change.
• Running on production data without testing. Batch tools that modify and save files should always be tested on copies.
• Leaving SOLIDWORKS sessions running. Standalone tools should manage the application lifecycle and clean up.
• Skipping the process question. Automating a poor process only creates faster errors.
• Ignoring PDM. Writing to files in a vault without respecting check-out and revisions causes conflicts.
When to Buy, Configure or Build
Custom code gives maximum flexibility, but it has a maintenance cost. Before writing code, check whether a built-in feature (design tables, Task Scheduler, Design Checker), a configurator such as DriveWorks, or a third-party add-in already solves the problem. Build custom when your rules are specific to your business, and when the value of the automation justifies ongoing support.
Conclusion
The SOLIDWORKS API turns SOLIDWORKS from a design tool into a platform you can integrate into your wider engineering process. VBA is a quick way to prototype and to automate individual tasks. C# add-ins and standalone applications suit shared, maintained tools and integration with other systems. The success factors are the same in both cases: a clear process, robust error handling, careful testing and good documentation.
If your team is planning API-based automation, or needs help choosing between built-in tools, configurators and custom development, speak with the engineering software experts at Panshul Technologies. We can help you assess your workflow and connect it with SOLIDWORKS CAD, data management and training.
FAQ Section
Do I need to be a programmer to use the SOLIDWORKS API?
Basic recorded macros need little programming knowledge, but reliable tools need programming skills such as variables, loops, error handling and object-oriented design. Engineers with some VBA or C# experience can learn quickly with examples.
Should I use VBA or C# for SOLIDWORKS automation?
Use VBA for quick personal tools and prototypes. Use C# when you need a shared add-in, better structure, easier maintenance, or integration with other systems.
Can the SOLIDWORKS API run without opening the SOLIDWORKS interface?
A standalone application can start SOLIDWORKS in the background and control it, though it still runs the SOLIDWORKS application. The Document Manager API can read file data without launching SOLIDWORKS, but with a narrower set of functions and its own licence key.
Is a macro the same as an add-in?
No. A macro is a script run on demand from within SOLIDWORKS. An add-in is a compiled component loaded by SOLIDWORKS, which can add menus, toolbars and panels and can be more stable and faster.
Can I automate SOLIDWORKS PDM with macros?
No. The PDM API is used through add-ins and standalone applications written in languages such as C# or VB.NET, with no PDM macros.
Will my macros still work after upgrading SOLIDWORKS?
Most will, but some API calls are replaced by newer versions, and behaviour can change. Test your tools on each new release, and read the API release notes before upgrading.
What is the best way to learn the SOLIDWORKS API?
Record a macro for a task, read the generated code, look up each call in the API Help, then rebuild it as clean, parameterized code. Start with small tools and expand.
Can the API create a complete model from a customer order?
It can, if the geometry follows clear rules. For many configure-to-order products, a parametric model driven by a design table or a rules-based tool is easier to maintain than creating geometry in code.