← All days ◀ Day 29Day 31 ▶
SolidWorks Course Part II · Week 5 · Day 30

Drafting Macros with an AI Assistant

An AI assistant can draft a macro in seconds — and invent API calls that do not exist. Learn to ask well, then review and test like an engineer.

⏱ about 2–3 hours🧪 Lab: Bug hunt❓ 5-question quiz

1Today's goals

  • Write a precise prompt for a SolidWorks macro
  • Recognise invented API calls and logic bugs
  • Review with a checklist and test on copies
  • Know what not to paste into an AI tool

2Draft fast, check slowly

AI assistants (Claude and others) are good at the shape of a macro: loops, file handling, error checks, comments. They are less reliable on exact SolidWorks API names, argument order and version differences — they may produce a plausible call that does not exist. Treat AI code like code from a new colleague: useful, reviewed before use.

Give contextSolidWorks version, VBA or VB.NET, document types, folder layout, what "done" looks like.
Give constraints"Use only calls from the SolidWorks API Help", "no deletion", "log every file", "stop on first error".
Ask for checksNull checks after every Open, error codes printed, a dry-run mode that only lists files.
VerifyLook up every API call in API Help; run on copies; read the log.
Confidentiality: follow your organisation's rules on AI tools. Never paste classified, export-controlled, customer-confidential or personal data into a tool that is not approved for it. For macros you rarely need real data — use dummy folder names and part names.

Prompt template

You are helping me write a SolidWorks VBA macro (.swp) for SolidWorks [2024].
Goal: [export every .SLDPRT in a folder to STEP AP214 in the same folder].
Inputs: folder path in a constant at the top. Files: [parts only].
Rules:
- Use only methods documented in the SolidWorks API Help; name the interface for each call.
- Check every object for Nothing before using it; print errors and warnings codes.
- Never delete or overwrite source files. Close each document after use.
- Add a DRY_RUN constant: when True, only list what would be done.
- Comment each block in plain English.
Finish with a list of API calls used, so I can check each one in API Help.

3Do it in SolidWorks

  1. Write your prompt with the template; ask your approved AI assistant for the macro.
  2. Paste it into a new macro. Turn on Option Explicit and Debug ▸ Compile VBAProject — compile errors catch invented names and missing variables.
  3. Look up every API call it lists in Help ▸ API Help: interface, arguments, return value.
  4. Run with DRY_RUN = True on a copy folder. Read the list.
  5. Run for real on the copy. Open three outputs and check them.
  6. Ask the AI to explain any line you do not understand — then confirm in API Help.
  7. Save the reviewed macro with a header block and a note "reviewed by / date".

4Lab: bug hunt

An AI-drafted macro with four real problems — including one invented API method. Find them before you check.

5Pro tips and common mistakes

  • Compile first: Method or data member not found usually means an invented or wrong-interface call.
  • Ask for small macros and combine them yourself; long AI programs hide more mistakes.
  • When the AI fixes a bug, re-run the whole test — fixes often break something else.
  • Keep a library of reviewed macros; reuse beats regenerating.

6Practice

Ask an AI assistant for a macro that renames the custom property PartNo to PartNumber in every part of a folder, keeping the value. Review it with the checklist above and the lab's four bug types, test on copies, and record what you had to fix.

7Quick quiz

Five questions. Click an answer — you get the explanation straight away.

Progress is kept in this browser only.